简体   繁体   English

如何使用XmlHttpRequest和ASP.NET MVC处理文件上传错误?

[英]How to deal file upload error with XmlHttpRequest and asp.net mvc?

for simplicity: 为简单起见:

client: 客户:

var xrh = new XmlHttpRequest();
...
xrh.onerror = on_upload_error;
xrh.send(...)

server: 服务器:

public class file_transfer
{
    [HttpPost]
    public void Upload(HttpPostedFileBase[] files_to_upload)
    {
        ...
        if (succeeded)
        {
            // what should I to to let the client know the success? in onload
        }
        else
        {
            // what should I to to let the client know the failaure? maybe in onerror
        }
    }
}

what should I to to let the client know the success? 我应该如何让客户知道成功? in onload? 在加载?

what should I to to let the client know the failaure? 我应该如何让客户知道失败的原因? maybe in onerror? 也许陷入错误?

You can return http success/failed code to client: 您可以将http成功/失败的代码返回给客户端:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase[] files_to_upload)
{
    ...
    if (succeeded)
    {
        return new HttpStatusCodeResult(200);
    }
    else
    {
        return new HttpStatusCodeResult(400);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM