带有xhrHttpRequest文件上传的.NET WebMethod [英] .NET WebMethod with xhrHttpRequest File Upload

查看:133
本文介绍了带有xhrHttpRequest文件上传的.NET WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript结合VB.NET中的WebMethod进行异步文件上传

I am trying to do an asynchronous file upload using JavaScript in combination with a WebMethod in VB.NET

JavaScript:

JavaScript:

xhr.open('POST', "upload.aspx/upload", true);
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");

var formData = new FormData();

// append the files
for (var i in files) {
   formData.append(base.el.name, files[i]);
}

xhr.send(formData);

VB.NET:

<Web.Services.WebMethod(enableSession:=True)> _
Public Shared Function upload() As String
    Return "Hello World!"
End Function

如果我使用 content-type = application/x-www-form-urlencoded multipart/form-data ,则使用我的WebMethod不会成功 content-type = application/json ,则单击WebMethod,但响应为500,并显示以下消息:无效的JSON原语:------ WebKitFormBoundary ...

If I use content-type=application/x-www-form-urlencoded, or multipart/form-data the WebMethod does not get hit, if I use content-type=application/json, the WebMethod is hit, but the response is a 500 with message: Invalid JSON primitive: ------WebKitFormBoundary ...

有没有一种方法可以使AJAX WebMethod与多部分表单数据一起使用?

Is there a way to make the AJAX WebMethod work with multipart form data?

推荐答案

似乎.NET不允许 multipart/form-data 用于内容类型:

Seems .NET will not allow the multipart/form-data for the content-type:

JSON劫持以及ASP.NET AJAX 1.0如何避免这些攻击

ASP.NET有一个内置的验证保护层 对基于GET和POST的ASP.NET AJAX Web方法都强制实施 是无论使用什么HTTP动词, ASP.NET始终 要求将HTTP Content-Type标头设置为该值 application/json .如果没有发送此内容类型头,ASP.NET AJAX将拒绝服务器上的请求.

There is a built-in validation layer of protection that ASP.NET enforces for both GET and POST based ASP.NET AJAX web methods, which is that regardless of the HTTP verb being used, ASP.NET always requires that the HTTP Content-Type header is set to the value application/json. It this content type header is not sent, ASP.NET AJAX will reject the request on the server.

似乎最好的方法是改用通用的.ashx文件处理

Seems the best way is to handle this using a generic .ashx file instead

这篇关于带有xhrHttpRequest文件上传的.NET WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆