【发布时间】:2014-07-14 13:28:09
【问题描述】:
我正在尝试将文件添加到我的数据库中,但由于某种原因它无法正常工作,有人可以告诉我为什么。这是我的沙发:
$(document).on("click", "#BUTTON", function () {
var FILE= $('#name').val();
$.ajax({
type: "POST",
dataType: 'json',
url: "@Url.Action("File", "Controller")",
data: JSON.stringify(FILE),
contentType: "application/json; charset=utf-8",
success: function () {
}
});
});
这是在我的控制器中发生的:
public ActionResult File(HttpPostedFileBase file)
{
if (file != null)
{
string pic = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(Server.MapPath("~/path"), pic);
// file is uploaded
file.SaveAs(path);
// save the image path path to the database or you can send image
// directly to database
// in-case if you want to store byte[] ie. for DB
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
}
}
// after successfully uploading redirect the user
return Index();
}
}
当我在HttpPostedFileBase file 行中放置一个刹车点时,文件有空值,我不知道为什么?如果我这样做alert(FILE);,那么我将获得完整的文件路径,例如G:/filename/imagename.jpg 但在控制器中路径没有被传递?
【问题讨论】:
-
.val()只返回文件的字符串名称。 @Steven,不是真的,有ajax方法可以上传 -
@Steven 您可以使用 ajax 上传文件。也许在旧的浏览器中它不起作用,但新的浏览器可以。