我有以下方法上传文件:
[HttpPost]
public ActionResult Upload()
{
string directory = @"C:\Temp\";
HttpPostedFileBase file = Request.Files["file"];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
file.SaveAs(Path.Combine(directory, fileName));
}
return RedirectToAction("Index");
}
这是我的ajax电话:
$('#uploadButton').on('click', function()
{
$.ajax({
type: "POST",
dataType: 'json',
url: '@Url.Action("Upload", "Application")',
timeout: 2000,
contentType: 'application/json',
success: function (data) {
//show content
alert('Success!')
}
});
}
)
我需要使用上传的文件发送“Request.files [”file“]”。
以下是我的代码所在的表单:
<form action="/profile/upload" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>File</label>
<input type="file" name="file" id="file" class="form-control" />
<br />
<input type="button" id="uploadButton" value="Upload" />
</div>
</form>
答案 0 :(得分:1)
试试这个:
null