Opera中基于form / iframe的文件上传问题 [英] issues with form/iframe based file upload in Opera
问题描述
解决方案
当浏览器不支持XMLHttpRequest上传(Opera)时,我使用表单/ iframe技术向服务器发送文件。
$ b $ ol <
< iframe src =javascript:假; name =file-iframe>< / iframe>
< form enctype =multipart / form -datamethod =POST>
- set target = iframe's name,
target = file-iframe
- set action = url,
action =/ upload
提交表单时,服务器吐出一个异常:
org .apache.commons.fileupload.MultipartStream $ MalformedStreamException:在org.apache.commons.fileupload流意外结束
在org.apache.commons.fileupload.MultipartStream.readHeaders(MultipartStream.java:538)
。 FileUploadBase $ FileItemIteratorImpl.findNextItem(FileUploadBase.java:999)LT
。在org.apache.commons.fileupload.FileUploadBase $ FileItemIteratorImpl&;初始化>(FileUploadBase.java:965)
。在org.apache.commons .fileupload.FileUploadBase.getItemIterator( FileUploadBase.java:331)在org.apache.commons.fileupload.servlet.ServletFileUpload.getItemIterator(ServletFileUpload.java:148
)
在com.beamit.jetty.UploadServlet.doPost(UploadServlet.java:在javax.servlet.http.HttpServlet.service 28)在javax.servlet.http.HttpServlet.service
(HttpServlet.java:727)
(HttpServlet.java:820)
.. 。
...
以下是
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response){
ServletFileUpload up = new ServletFileUpload();
尝试{
FileItemIterator iter = up.getItemIterator(request); //< ------ ERROR
...
} catch(IOException e){
e.printStackTrace();
}
...
我一直在尝试调试小时,我无处可去。任何想法,为什么这可能会发生?
我可以使用不同的浏览器提供工作/非工作文件上传的HTTP请求,如果需要,还可以提供更多的源代码。
$ b
更多信息
在 Chrome 中为简单的hello world文本文件提出的HTTP请求。
POST / upload / 0145c HTTP / 1.1
主机:beamit:8080
连接:keep-alive
缓存控制:最大年龄= 0
的User-Agent:Mozilla的/ 5.0(Macintosh上;英特尔的Mac OS X 10_7_2)为AppleWebKit / 535.1(KHTML,例如Gecko)铬/ 14.0.835.202 Safari浏览器/ 535.1
接受:text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8
接受编码:gzip,deflate,sdch
Accept-Language:en -US,en; q = 0.8
Accept-Charset:ISO-8859-1,utf-8; q = 0.7,*; q = 0.3
引用:http:// beamit:8080 /
Content-Length:44
Origin:http:// beamit:8080
Content-Type:multipart / form-data; HTTP请求一个简单的hello world文本文件在 边界上的一个简单的hello world文本文件的HTTP请求 border = ---- WebKitFormBoundaryQes3eWoFE2iw6kGE
> Opera 。
POST / upload / f889b HTTP / 1.1
User-Agent:Opera / 9.80 (Macintosh; Intel Mac OS X 10.7.2; U; en)Presto / 2.9.168版本/11.52
主机:beamit:8080
Accept:text / html,application / xml; q = 0.9, application / xhtml + xml,image / png,image / webp,image / jpeg,image / gif,image / x-xbitmap,* / *; q = 0.1
Accept-Language:en,en-US; q = 0.9,JA; q = 0.8,FR,q = 0.7,DE; q = 0.6,ES; q = 0.5,它; q = 0.4,PT; q = 0.3,PT-PT; q = 0.2,NL; q = 0.1,SV; q = 0.1,NB; q = 0.1,哒; q = 0.1,网络连接; q = 0.1,如; q = 0.1,PL; q = 0.1,ZH-CN; q = 0.1,ZH-TW ; q = 0.1,ko; q = 0.1,ar; q = 0.1,cs; q = 0.1,hu; q = 0.1,tr; q = 0.1
接受编码:gzip,deflate
Referer :http:// beamit:8080 /
Cookie:__utma = 248039316.1622718495.1317335167.1319648741.1319655028.19; __utmb = 248039316.4.10.1319655028; __utmc = 248039316; __utmz = 248039316.1317335167.1.1.utmcsr =(直接)| utmccn =(直接)| utmcmd =(无)
连接:保持活动
的Content-Length:76
内容类型:多/格式数据; border = ---------- cah2BDOhbumylzxVY398ZJ
<我找出了问题所在。剥离所有不必要的代码和标记后,我剩下一个裸机/ iframe文件上传页面。
当我动态构建表单时,我将文件输入元素作为子元素插入:
< input type =fileid =file-select-input/>
...我有这样的事情:
< form enctype =multipart / form-dataid =file-select-formtarget =select-file-iframemethod =POSTaction = /上传/ >
< div id =file-select-buttonclass =>
< input type =fileid =file-select-input/>
< / div>
< / form>
< iframe style =display:noneid =select-file-iframesrc =javascript:false; NAME = 选择文件的iframe >< / iframe中>
这个设置给了我这个问题。经过一番调查后发现,当我提交上面的表格时,输入的文件信息没有被发送。原因是,我没有在文件输入元素上指定的名称
属性。所以当我改变它的时候:
< input type =fileid =file-select-inputname =file/>
...事情奏效。
SOLUTION
Refer to my answer below: issues with form/iframe based file upload in Opera
I'm using the form/iframe technique to send files to the server whenever a browser does not support XMLHttpRequest upload (Opera). On a high level, here's the setup...
- create an iframe
<iframe src="javascript:false;" name="file-iframe"></iframe>
- create a form
<form enctype="multipart/form-data" method="POST">
- set target = iframe's name,
target="file-iframe"
- set action = url,
action="/upload"
- set target = iframe's name,
- add the file input to the form
- submit form
When the form is submitted, the server spits out an exception:
org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream.readHeaders(MultipartStream.java:538)
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:999)
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:965)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
at org.apache.commons.fileupload.servlet.ServletFileUpload.getItemIterator(ServletFileUpload.java:148)
at com.beamit.jetty.UploadServlet.doPost(UploadServlet.java:28)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
...
...
Here is a code snippet in the UploadServlet.java
file, where the failure happens in the last line of the snippet:
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
ServletFileUpload up = new ServletFileUpload();
try {
FileItemIterator iter = up.getItemIterator(request); // <------ ERROR
...
} catch (IOException e) {
e.printStackTrace();
}
...
I've been trying to debug this for hours and I'm getting nowhere. Any ideas as to why this might be happening?
I can provide the HTTP requests for working/non-working file uploads using a different browser, as well as some more source code if needed.
MORE INFO
HTTP request for a simple "hello world" text file in Chrome.
POST /upload/0145c HTTP/1.1
Host: beamit:8080
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
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
Referer: http://beamit:8080/
Content-Length: 44
Origin: http://beamit:8080
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryQes3eWoFE2iw6kGE
HTTP request for a simple "hello world" text file in Opera.
POST /upload/f889b HTTP/1.1
User-Agent: Opera/9.80 (Macintosh; Intel Mac OS X 10.7.2; U; en) Presto/2.9.168 Version/11.52
Host: beamit:8080
Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1
Accept-Language: en,en-US;q=0.9,ja;q=0.8,fr;q=0.7,de;q=0.6,es;q=0.5,it;q=0.4,pt;q=0.3,pt-PT;q=0.2,nl;q=0.1,sv;q=0.1,nb;q=0.1,da;q=0.1,fi;q=0.1,ru;q=0.1,pl;q=0.1,zh-CN;q=0.1,zh-TW;q=0.1,ko;q=0.1,ar;q=0.1,cs;q=0.1,hu;q=0.1,tr;q=0.1
Accept-Encoding: gzip, deflate
Referer: http://beamit:8080/
Cookie: __utma=248039316.1622718495.1317335167.1319648741.1319655028.19; __utmb=248039316.4.10.1319655028; __utmc=248039316; __utmz=248039316.1317335167.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Connection: Keep-Alive
Content-Length: 76
Content-Type: multipart/form-data; boundary=----------cah2BDOhbumylzxVY398ZJ
I figured out what the issue was. After stripping off all unnecessary code and markup, I was left with a bare-bones form/iframe file upload page.
When I am building the form dynamically, I insert the file input element as a child:
<input type="file" id="file-select-input" />
... and I had something like this:
<form enctype="multipart/form-data" id="file-select-form" target="select-file-iframe" method="POST" action="/upload/">
<div id="file-select-button" class="">
<input type="file" id="file-select-input" />
</div>
</form>
<iframe style="display: none" id="select-file-iframe" src="javascript:false;" name="select-file-iframe"></iframe>
This setup was giving me the issues. After some investigation, it turns out that when I submit the above form, the input file information was not being sent. The reason for this, it turns out, is that I didn't have the name
attribute specified on the file input element. So when I changed it to this:
<input type="file" id="file-select-input" name="file" />
... things worked.
这篇关于Opera中基于form / iframe的文件上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!