【问题标题】:Send raw file data from input type=file over ajax通过ajax从输入类型=文件发送原始文件数据
【发布时间】:2012-03-12 19:21:57
【问题描述】:

我有一个简单的表格:

<form enctype="multipart/form-data" id="imageupload">
   <input name="files" type="file" />
   <input type="button" value="Upload" />
</form>

现在我想通过 ajax 请求发送所有文件。

此示例有效,但有一个错误。在我保存的文件中还有其他信息:

-----------------------------169443243924626
Content-Disposition: form-data; name="files"; filename="shelby.png"
Content-Type: image/png


       $.ajax({
       url: 'imageupload.php',  //server script to process data
       type: 'POST',
       xhr: function() {  // custom xhr
           myXhr = $.ajaxSettings.xhr();
           if(myXhr.upload){ // check if upload property exists
               myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
           }
           return myXhr;
       },
       //Ajax events
       //beforeSend: beforeSendHandler,
       //success: completeHandler,
       //error: errorHandler,
       // Form data
       data: new FormData($('#imageupload')[0]),
       //Options to tell JQuery not to process data or worry about content-type
       cache: false,
       contentType: 'multipart/form-data',
       processData: false
   });

现在我开始了:

$('#imageupload')[0].files.files[0]

我可以给我们 .name 我知道这个名字。但是如何获取原始文件数据?

【问题讨论】:

    标签: php jquery ajax file-upload


    【解决方案1】:
    try this
    
     $filename =  $_FILES['ur_image']['name'] ;
     $filesize =  $_FILES['ur_image']['size']; 
     $erro     =  $_FILES['ur_image']['error']; //checks UPLOAD_ERR_OK
     $tmpname  = $_FILES['ur_image']['tmp_name'];              
     $dest     = ROOT_DIR.'/upload/logo/';
    

    【讨论】:

      【解决方案2】:

      原始文件数据无法通过这种方式获得,在 HTML 5 之前,javascript API 阻止了对文件系统上文件的访问。

      现在在现代浏览器中,您可以使用javascript file API

      【讨论】:

        【解决方案3】:

        它可以通过 HTML 5 的文件 api 来完成(如前所述),但仍然相当困难。作为非官方标准的方式是将表单的结果发布到隐藏的 iframe 中并通过 javascript 提交表单。当帖子在 iframe 中加载时,您会包含调用在父窗口上定义的回调函数的标签,例如 window.parent.doSomething();

        How do you post to an iframe?

        是的,这是非常愚蠢的,但它确实是这样做的。

        【讨论】:

          猜你喜欢
          • 2015-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-16
          • 2013-05-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多