HTML:
<input type="file" id="projectFile" name="projectFile"/>
javascript:
$(function () {
$('#projectFile').live('change', function () {
$.ajaxFileUpload({
url: '/Management/SaveProjrctFile/', //需要链接到服务器地址
secureuri: false,
fileElementId:'projectFile', //文件选择框的id属性
dataType:'json', //服务器返回的格式,可以是json
success: function (data, status) //相当于java中try语句块的用法
{
alert("OK" + data.name + "-" + data.path);
},
error: function (data, status, e) //相当于java中catch语句块的用法
{
alert( e );
}
});
});
});
Action:
public ActionResult SaveProjrctFile(HttpPostedFileBase projectFile)
{
HttpPostedFileBase file = projectFile;
if (file != null)
{
try
{
string filePath = AppDomain.CurrentDomain.BaseDirectory + "Content\\UploadFiles";
string fileName = Path.GetFileName(file.FileName);
string savePath = string.Format(@"{0}\{1}", filePath, fileName);
file.SaveAs(savePath);
return Json(new { name = fileName });
}
catch (Exception ex)
{
return Json(new { error = ex });
}
}
else
{
return Json(new { error = "未选择文件"});
}
}
检查了没出现错误的“<”!
实验证明,直接返回Json不行,只能返回Content,Json形式的字符串
我出现了 Uncaught SyntaxError: Unexpected end of input