如何使用XMLHttpRequest和FormData对象在multipart/form-data上设置自己的边界 [英] How to set my own boundary on multipart/form-data using XMLHttpRequest and FormData object

查看:215
本文介绍了如何使用XMLHttpRequest和FormData对象在multipart/form-data上设置自己的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用FormData对象将文件追加到请求时,我一直在尝试寻找一种在XMLHttpRequest对象上设置自己的边界的方法.我已经看到了很多关于此的文章,每个人都说只是不设置边界,它将自动为您生成".这不是我想要的.让我解释一下我需要什么,这样我就不会得到这类答复.

I have been trying to find out a way to set my own boundary on an XMLHttpRequest object when using the FormData object to append files to a request. I've seen multiple posts on this and everyone says "just don't set the boundary and it will automatically generate for you." This is NOT what I want. Let me explain what I need so I don't get these kinds of responses.

我有一个Web服务终结点,向该终结点发送包含两个图像和一些json数据的multipart/form-data请求.由于WCF没有任何方法可以解析多部分请求,因此我使用一些开放源代码构建了自己的解析器.它的工作方式是,我定义一个边界,该边界用于分隔请求的每个部分,从那里开始一切正常.因此,为了使我的解析类能够在输入流中找到任何内容,我必须能够将边界设置为服务器代码所期望的确切范围.

I have a web service endpoint to which I send a multipart/form-data request with two images and some json data. Since WCF doesn't have any way to parse through a multipart request, I built my own parser using some open source code. The way it works is, I define a boundary which is used for separating each section of the request and from there everything works hunky dory. As such, I have to be able to set the boundary to exactly what the server code expects in order for my parsing classes to be able to find anything in the input stream.

我知道这是可以做到的,因为我已经使用Fiddler做到了这一点,并且同事能够在我们正在构建的调用我方法的应用程序中做到这一点,但是我正在尝试使用Chrome浏览器应用程序使其工作称为Postman的邮递员,它使用FormData对象发送多部分请求.除了产生请求自己的边界(通常是这样的事实)以外,它还起作用:

I know this can be done because I've done so using Fiddler and a coworker is able to do so in the app we are building which calls my method, but I'm trying to get it working using a Chrome browser app called Postman which sends multipart requests using the FormData object. It is working other than the fact that the requests generate their own boundary which usually is something like:

----WebKitFormBoundaryQUWQnB6c7TzNzdcz

并且结尾处的附加字符串是随机生成的,因此它永远不会相同,因此我无法使用此工具来测试我的端点,因为服务器无法知道要寻找的边界.

And the appended string on the end is randomly generated so its never the same, hence I can not use this tool to test my endpoint because the server has no way of knowing what boundary to look for.

我尝试在Content-Type标头中设置边界,并且当请求显示标头已添加到请求中时,主体仍使用随机边界.

I've tried setting the Content-Type header with my boundary in it and while the request shows the header being added to the request, the body still uses the random boundary.

所以问题是如何告诉FormData对象和/或XMLHttpRequest对象使用我的边界,而不是正在生成的随机边界?

So the question is how can I tell the FormData object and/or the XMLHttpRequest object to use my boundary instead of the random one that is being generated?

我无法想象这是一件不常见的事情,直到现在我所有使用multipart/form-data调用服务的经验都告诉我如何在api中设置边界,而没有人说过只是不设置它,我们将使用生成的随机垃圾..."

I can't imagine this is an uncommon thing to want to do, I mean up until now all my experience calling services that use multipart/form-data tell me what to set the boundary to in the api and nobody has said "just don't set it and we'll use the random junk that is generated..."

这也是视觉效果,这是我在标题中看到的内容:

Also as a visual, here is what I'm seeing in the headers:

Request Headers
POST /DHICachet.svc/json/DepositCheck HTTP/1.1
Host: dhiibews.securexfr.com
Connection: keep-alive
Content-Length: 514696
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Content-Type: multipart/form-data; boundary=myboundary
Cache-Control: no-cache
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ASP.NET_SessionId=zdepe2nhkz0vbhxiulzc2qq1

Request Payload
------WebKitFormBoundaryQUWQnB6c7TzNzdcz
Content-Disposition: form-data; name="item"; filename="Screen shot 2012-09-03 at 4.00.10 AM.png"
Content-Type: image/png


------WebKitFormBoundaryQUWQnB6c7TzNzdcz--

请注意,即使我的边界在请求标头中设置为myboundary,主体也会将生成的所有边界置于幕后.另外请注意,如果我未设置该标头,则会将其与其中的随机边界.

Notice that even though my boundary is set to myboundary in the request header, the body just puts whatever boundary is generated behind the scenes in. Also note that if I don't set that header it just puts it in with the random boundary in it.

推荐答案

这并不能真正回答我的问题,但是我确实找到了解决方法.我突然意识到,通过对内容类型标头进行一些字符串数学运算,我实际上可以检测到所遇到的边界.我刚刚在代码中添加了以下内容:

This doesn't really answer my question, but I did find a work around. It dawned on me that I could actually detect the boundary that was coming through by doing some string math on the content-type header. I just added the following to my code:

string contenttype = _ctx.IncomingRequest.Headers["Content-Type"].ToString();
string boundary = contenttype.Substring(contenttype.IndexOf('=') + 1);

这使我可以接受任何调试边界.但是,我仍然需要特定的生产边界.

This allows me to accept any boundary for debugging. I will still require our specific boundary for production however.

这篇关于如何使用XMLHttpRequest和FormData对象在multipart/form-data上设置自己的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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