我想通过ajax将文件上传到controller.But在控制器方法参数中获取null。 控制器
public ActionResult Upload(HttpPostedFileBase file,int value)
{
try
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/File/"), fileName);
file.SaveAs(path);
_manager.UploadFile(file, path.ToString());
}
ViewBag.Message = "Upload successful";
return RedirectToAction("Index");
}
catch
{
ViewBag.Message = "Upload failed";
return RedirectToAction("Index");
}
}
在索引中我正在使用普通的输入文件类型并提交类型按钮。一切正常但控制器方法参数变为空为什么?
索引
<label class="btn btn-block btn-primary">
Browse …
<input type="file" name="file" class="btn" id="uploadfile" style="display: none;">
</label>
<input type="submit" class="btn" id="submit" value="Upload">
$(document).ready(function (e) {
$('#submit').click(function (event) {
var formData = new FormData();
var totalfiles = document.getElementById("uploadfile");
for (var i = 0; i < totalfiles; i++) {
var file = document.getElementById("uploadfile").files[i];
formData.append("uploadfile", file);
}
var dam=1;
dam = $("input:checkbox:checked").attr("value");
alert(dam);
alert(formData);
$.ajax(
{
url: '@Url.Action("Upload","Document")',
type: "POST",
data: { file: formData ,value:dam},
datatype: "multipart/form-data",
traditional: true,
success: function (response) {
// $("#file").html(response);
}
});
})
});