【问题标题】:Remote File upload in grailsgrails中的远程文件上传
【发布时间】:2009-03-05 08:22:45
【问题描述】:

我正在使用使用大量 ajax 的 grails 创建一个 web 应用程序。我想使用 ajax 实现文件上传。我不知道如何使用 ajax 进行文件上传。我的示例 GSP 代码是:

<!-- code for file upload form-->
<div id="updateArea">

</div>

我尝试了和。上传后我想用结果更新“updateArea”。结果我打算显示上传文件的详细信息。

【问题讨论】:

标签: ajax grails frameworks asyncfileupload


【解决方案1】:

实际上不可能通过 Ajax 上传文件。您仍然可以使用隐藏的 iframe 在后台上传文件并评估响应(然后在 iframe 内)或在上传完成后触发另一个 ajax 调用。

<g:form name="upload-form" action="upload" method="post" enctype="multipart/form-data" target="hidden-upload-frame">
    File: <input type="file" name="myFile" />
    <button type="submit">Upload</button>
</g:form>

<iframe id="hidden-upload-frame" name="hidden-upload-frame" style="display: none" onload="onUploadComplete">
</iframe>

<script type="text/javascript">
    function onUploadComplete(e) {
        // Handle upload complete
        alert("upload complete");
        // Evaluate iframe content or fire another ajax call to get the details for the previously uploaded file
    }
</script>

另一种选择是使用基于 Flash 的上传机制(例如 swfupload)而不是 iframe。

【讨论】:

  • 嗨 Siegfried Puchbauer。我试过你的答案。但 iframe 在 mozilla firefox 和 IE 浏览器中不起作用。我尝试使用 google chrome 浏览器。它正在工作。如何在 IE 和 firefox 中进行这项工作?
  • 嗨。对不起,这是一个错字。通常您必须定义 iframe 的 name 属性,而不是像我无意中那样定义 id 属性。干杯
  • 非常感谢您的回答和快速响应。现在问题解决了。
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
  • 2014-05-15
  • 1970-01-01
  • 2012-04-26
相关资源
最近更新 更多