天天看点

AjaxFilUpLoad文件正常上传 但前台无发接收到 后台返回的Json数据

在开发过程中当需要使用WEBAPI上传文件时,如果需要将文件的保存路径或者文件名等信息返回到前台去,后台已经可以将文件保存到指定的文件夹中,当返回数据的时候,前台无法接收到Json数据或者提示Resource interpreted as Document but transferred with MIME type application/xml:或者报后台500。

解决方式:

不使用WEBAPI的方式使用Controller的Action:

创建一个类来保存返回前台的数据。保存成功后将需要的数据保存到一个model中然后

return View("FileUpLoad", model);
           
[Serializable]
    public class FileModel
    {
        public string FileName { get; set; }
        public string FilePath { get; set; }
        public bool UpSucceed { get; set; }
    }
           

创建的视图的表示也很简单:只保存需要的信息即可

@model  SmilePodAPI.Controllers.FileModel
@{
    Layout = null;
}
{
FileName : '@Model.FileName',
FilePath:'@Model.FilePath',
UpSucceed:'@Model.UpSucceed'
}


           

因为文件上传成功后会有一个成功的回调函数在里面就会返回这些数据再做其他的处理。。。

继续阅读