S3浏览器通过POST上传:无法优雅地处理错误
我正在编写一个应用程序,希望客户能够直接从浏览器上传到 Amazon S3。我可以把这个工作做得很好。但是,当发生错误时,我希望能够更优雅地处理它们,而不是将 XML 文档散布在客户的屏幕上。
我有一个我认为可行的计划,但它失败了。这就是我正在尝试的:
- 创建一个表单来执行上传,并将表单存储在 S3 本身上,与表单的“action”属性位于同一域中。
- 将客户重定向到此表单。现在他们的浏览器位于 https://
.s3.amazonaws.com/something 。 - 该页面包含一个隐藏的 iframe。该表单将其目标设置为 iframe。
- load 事件处理程序查看 iframe 的内容并对其进行操作。
所以,像这样:
<iframe id="foo" name="foo" style="display: none" />
<form target="foo" action="https://<bucket>.s3.amazonaws.com/">
<input type="hidden" name="..." value="..." />
<input type="file" name="file" />
</form>
使用这个 javascript(使用 jquery):
function handler() {
var message = $("#foo").contents().find('message').text();
alert(message);
}
$("#foo").load(handler);
使用 firebug,我可以看到 iframe 包含一个 XML 文档,其中包含一个“消息”节点。但是,.find('message')
始终无法在 XML 文档中找到任何内容。
请注意,表单的操作与文档本身具有相同的域、端口和方案。因此,我认为我不应该违反同源策略。正确的?但每次都失败。这是使用 Firefox 和 Google Chrome 浏览器。
感谢您的任何建议!
I am writing an app where I want the customer to be able to upload to Amazon S3 straight from the browser. I can make this work just fine. But when errors occur, I want to handle them more gracefully than splattering an XML document on the customer's screen.
I have a scheme that I think would work, but it's failing. Here's what I'm trying:
- Create a form to do the upload, and store the form on S3 itself, in the same domain as the "action" attribute of the form.
- Redirect the customer to this form. Now their browser is sitting on https://<bucket>.s3.amazonaws.com/something.
- The page contains a hidden iframe. The form sets its target to the iframe.
- The load event handler looks at the contents of the iframe, and acts upon it.
So, something like this:
<iframe id="foo" name="foo" style="display: none" />
<form target="foo" action="https://<bucket>.s3.amazonaws.com/">
<input type="hidden" name="..." value="..." />
<input type="file" name="file" />
</form>
with this javascript (using jquery):
function handler() {
var message = $("#foo").contents().find('message').text();
alert(message);
}
$("#foo").load(handler);
Using firebug, I can see that the iframe contains an XML document, that contains a "message" node. However, the .find('message')
always fails to find anything within the XML document.
Notice that the action of the form has the same domain, port, and scheme as the document itself. So, I don't think that I should be running afoul of the same-origin policy. Right? But it fails every time. This is using Firefox and Google Chrome browsers.
Thanks for any advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有许多 jQuery 插件可以有效地实现您想要做的事情。在 Google 上搜索“jQuery ajax-upload”,您可以直接使用这些控件之一来捕获帖子的结果或检查它们的代码并推出您自己的代码。
如果您要上传大文件,您可能还需要研究一些基于 Flash 的上传程序。纯形式的上传方法没有任何实现上传进度的方法,至少在Flash确实提供的新文件功能在浏览器中广泛传播之前是这样。 http://github.com/slaskis/s3upload#readme 是一个很好的基于 Flash 的文件上传器专为 S3 设计,为您提供 JavaScript 回调函数来处理错误、进度等。
There are a number of jQuery plugins that effectively implement exactly what you are trying to do. Do a Google search for "jQuery ajax-upload", you could just use one of these controls out of the box to capture the result of the post or examine their code and roll your own.
You may also want to look into some of the Flash based uploaders if you are uploading large files. The pure form method of uploading does not have any way of implementing an upload progress, at least not until the new file functions are wide spread in browsers, which Flash does offer. http://github.com/slaskis/s3upload#readme is a good flash based file uploader specifically designed for S3 and gives you call back functions in JavaScript to handle errors, progress and more.
这应该只是一条注释,但是您可以发布响应 XML 代码吗?
编辑:为了缩小范围,我做了一个简单的测试并且工作得很好:
然后我在我的 firebug 上做了一些 jQuery 代码:
也许你可以给出一个
alert($("#foo").html() )
并检查响应..This should be just a comment, but can you post the response XML code?
EDIT: Just to narrow down a little, i made a simple test and worked really fine:
Then I did some jQuery code on my firebug:
Maybe you can give an
alert($("#foo").html())
and check the response..我认为您正在尝试解决错误的问题。问题不在于 iframe 等细节问题,而在于您希望对上传进行更精细的控制。将文件上传到您的服务器,然后向亚马逊服务器提交请求,您将完全控制正在发生的事情。
当然,要复杂得多,但这就是控制的代价。
I think that you're trying to solve the wrong problem. The problem is not a detail one, with iframes and such, but that you want to have finer control over the upload. Upload the file to your server, then submit a request to the Amazon server, and you'll have absolute control over what's going on.
Significantly more complex, of course, but that's the price of control.