您好我正在使用jquery文件上传显示失败我已尝试使用diffeerent数据类型但仍显示失败,我可以显示上传的图像但完成事件未触发,失败事件正在触发。
$('#frmsettings').fileupload({
type: 'POST',
dataType: 'application/json',
url: '/Settings/UploadUiLogo',
add: function (e, data)
{
data.submit();
},
progressall: function (e, data)
{
},
done: function (e, data)
{
$.each(data.files, function (index, file)
{
alert("Done called");
});
},
fail: function (e, data)
{
alert("Fail : Called");
//window.location = JsErrorAction;
}
});
控制器
======================================
public ContentResult UploadUiLogo()
{
try
{
if (Request.Files != null)
{
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
string savedFileName = Path.Combine(Server.MapPath("~/Content/uploadlogo"), Path.GetFileName(Guid.NewGuid() + hpf.FileName));
hpf.SaveAs(savedFileName);
return Content("{\"name\":\"" + savedFileName + "\"", "application/json");
}
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}
请尽快帮助我。
答案 0 :(得分:0)
您的退货类型似乎有问题。试试这个:
// changed result type to JsonResult
public JsonResult UploadUiLogo()
{
try
{
if (Request.Files != null)
{
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
string savedFileName = Path.Combine(Server.MapPath("~/Content/uploadlogo"), Path.GetFileName(Guid.NewGuid() + hpf.FileName));
hpf.SaveAs(savedFileName);
//create a c# object rather than writing the string
var result = new { name = savedFileName };
//json result return
return Json(result);
}
}
return null;
}
catch (Exception ex)
{
throw ex;
}
}