weixin_33713707 2009-03-05 08:22 采纳率: 0%
浏览 139

如何使用ajax实现文件上传?

我正在使用ajax的grails创建一个Web应用程序,以实现文件上传。但是我不知道如何做?我的示例GSP代码是:

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

</div>

我尝试了with和.After上传,还更新了'updateArea',下面我打算试试上传文件的详细信息。

  • 写回答

2条回答 默认 最新

  • weixin_33735077 2009-03-05 09:06
    关注

    Uploading a file via Ajax is not really possible. You can still upload a file in the background using a hidden iframe and either evaluate the repsonse (which is then inside the iframe) or fire another ajax call after the upload is complete.

    <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>
    

    Another option is to use a flash based uploading mechanism (eg. swfupload) instead of the iframe.

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序