[英]How can I do a File Upload and return results to the same page?
I need to do a file upload and return the results to the same page. 我需要上传文件,并将结果返回到同一页面。 The
<form>
also has a couple of data fields that will be sent in the POST. <form>
还具有几个将在POST中发送的数据字段。
I can't use HTML 5 due to limited browser support, and unfortunately the AJAX file upload solutions I've seen all use HTML 5. I could do the POST in Javascript but I haven't seen a file upload done like this. 由于浏览器支持有限,我无法使用HTML 5,不幸的是,我看到的所有AJAX文件上传解决方案都使用HTML5。我可以使用Javascript执行POST,但没有看到这样的文件上传。
My server is written in Java but that shouldn't matter. 我的服务器是用Java编写的,但这无关紧要。
Is there a way to get the results of this kind of POST to return to the same page? 有没有办法让这种POST结果返回到同一页面?
I would suggest you take a look at how the jQuery Form Plugin does "legacy" file uploads. 我建议您看一下jQuery Form Plugin如何完成“旧版”文件上传。 Basically it uses an iframe to submit the form in the background.
基本上,它使用iframe在后台提交表单。 http://malsup.com/jquery/form/#file-upload
http://malsup.com/jquery/form/#file-upload
The generally accepted method outside of Flash, Silverlight, and the HTML5 File API, is to have your <form>
post to an invisible <iframe>
on the page. Flash,Silverlight和HTML5 File API之外的公认方法是将
<form>
发布到页面上不可见的<iframe>
上。
The <iframe>
location is the page or file handler that uploads your file under the hood. <iframe>
位置是在后台上传文件的页面或文件处理程序。 You then have a javascript function running on the main page, that polls your server periodically and checks to see if the download has been completed. 然后,您将在主页上运行一个javascript函数,该函数会定期轮询服务器并检查下载是否已完成。
When you upload the file to the <iframe>
, you need some way of identifying the upload that both the client and server know. 将文件上传到
<iframe>
,需要某种方式来识别客户端和服务器都知道的上传。 One way to do this is to generate a GUID on the client, and post it along with the file and your other data fields. 一种方法是在客户端上生成一个GUID,并将其与文件和其他数据字段一起发布。 The server should keep hold of that GUID before receiving the file data and track the progress of the upload by linking the progress and the GUID together.
在接收文件数据之前,服务器应保留该GUID,并通过将进度和GUID链接在一起来跟踪上载进度。 Since the client generated already knows the GUID, it can send requests to the server to get the progress of that upload by sending the what it generated.
由于生成的客户端已经知道GUID,因此可以通过发送生成的内容向服务器发送请求,以获取上载的进度。
I ended up using the jQuery Form Plugin . 我最终使用了jQuery Form Plugin 。 It supports sending basic auth credentials in the header with this option:
它支持使用此选项在标头中发送基本身份验证凭据:
headers : { Authorization: basicAuthCredentials}
which I also need to do. 我也需要做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.