I have a simple form which contains a input of type file which accepts only images.
I just want to post the file save it in table as well as server.
Below is my cshtml
@using (Html.BeginForm("UploadSlider", "Admin", FormMethod.Post, new { id = "frmUploadSliderImage", @class = "form-admin" }))
{
<h2 class="form-login-heading">upload images</h2>
<div class="login-wrap">
<span class="btn btn-default btn-file">
Browse <input type="file" id="sliderFile" multiple="multiple">
</span>
<span class="text-muted" id="filePlaceHolder">No files selected</span>
<span class="text-danger"></span>
<button class="btn btn-theme btn-block" name="upload" onclick="javascript: ValidateSliderImageandPost('frmUploadSliderImage', this);" id="btnUploadSliderImage" type="submit"><i class="fa fa-upload"></i> UPLOAD</button>
</div>
}
this is my js ajax part
function ValidateSliderImageandPost(form, ctrl) {
$("#" + form).on("submit", function (e) {
e.preventDefault();
var formContainer = $('#' + form + ' .text-danger');
var formdata = new FormData();
var fileInput = $('#sliderFile');
if ($(fileInput).get(0).files.length == 0)
{
$('.btn-file :file').parent().siblings().filter(".text-danger").html('Please select a file!');
}
if ($(formContainer).text().length == 0) {
run_waitMe('Uploading! Please wait...', 'stretch', '.container');
$.each($(fileInput).get(0).files, function (index,value) {
formdata.append($(this).attr('name'), $(this));
});
postData('UploadSlider', formdata, '.upslider .status');
if (msg) {
$(".container").find('#cont').waitMe('hide');
$("#" + form).find('input[type=text], textarea').val('').removeClass("alert-success");
}
else {
$(".container").find('#cont').waitMe('hide');
$("#" + form).find('input[type=text], textarea').removeClass("alert-success");
}
}
$("#" + form).unbind('submit');
return false;
});
}
function postData(url,data,target)
{
$.ajax({
url: url,
type: "POST",
dataType: 'json',
data: data,
processData: false,
contentType:false,
success: function (data) {
if (data.result) {
animateStatus("success", data.message, target);
msg = true;
}
else {
animateStatus("fail", data.message, target);
msg = false;
}
},
error:
function (data) {
animateStatus("fail", data.message, target);
msg = false;
}
});
}
This is my controller part
[HttpPost]
public JsonResult UploadSlider()
{
bool valid = false;
bool val = false;
if (Request.Files.Count > 0)
{
valid = true;
}
else
{
return Json(new { result = false, message = "Something went wrong! Please try again!" });
}
if (valid)
{
List<string> fil = new List<string>();
foreach (HttpPostedFileBase f in Request.Files)
{
HttpPostedFileBase file = f; //Uploaded file
string fileName = file.FileName;
fil.Add("./Images/Galllery/" + fileName);
System.IO.Stream fileContent = file.InputStream;
file.SaveAs(Server.MapPath("~/Images/Gallery/") + fileName);
}
using (var context = new MCBConnectionString())
{
foreach (string path in fil)
{
tbl_slider slider = new tbl_slider();
slider.slurl = path;
slider.slalt = "";
context.tbl_slider.Add(slider);
context.SaveChanges();
val = true;
}
}
if (val)
{
return Json(new { result = true, message = "Uploaded video successfully." });
}
else
{
return Json(new { result = false, message = "Could not upload video. Please try again!" });
}
}
return Json(new { result = false, message = "Could not upload video. Please try again!" });
}
When I debug and check the Request.Files.Count it will always be zero. Is there any alternate workaround or am I doing any mistakes in posting the file. I have followed this link and have made changes according to my needs.
See Question&Answers more detail:
os