将输入文件放入FormData jquery中提交给PHP。

时间:2022-11-23 18:47:50

I know already got question for this problem but i still cant did it properly. Need some help.

我知道这个问题已经有问题了,但我还是做不好。需要一些帮助。

I need to upload a file with additional data with it.

我需要上传一个带有附加数据的文件。

my input.php:

我的input.php:

<input type="file"  id="foto_path" name="foto_path"  />
<input type="button" value="Add" onclick="javascript:sendForm()" />

I send that with javascript :

我用javascript发送

function sendForm() {
    var fileInput = document.querySelector('#foto_path');
    var oMyForm = new FormData();
    var nip=123223374;//it will be generated by php, for temporary i just hardcode it
    oMyForm.append("foto_path", fileInput);
    oMyForm.append("nip",nip );
    var oReq = new XMLHttpRequest();
    oReq.open("POST", "upload-file.php", true);
    oReq.onload = function(oEvent) {
        if (oReq.status == 200) {
          //oOutput.innerHTML = "Uploaded!";
          alert('success');
        } else {
          //oOutput.innerHTML = "Error " + oReq.status + " occurred uploading your file.<br \/>";
          alert('failed');
        }
      };
    oReq.send(oMyForm);
}

and when I send that to upload-file.php:

当我将它发送到上传文件。php时:

logapp("post -> ".print_r($_POST,true));//logapp is just function to log to file
logapp("files -> ".print_r($_FILES,true));

I got this from log:

我从log中得到的

09/07/2013 02:47:06 pm :: post -> Array
(
    [foto_path] => [object HTMLImageElement]
    [nip] => 123223374
)

09/07/2013 02:47:06 pm :: files -> Array
(

)

I got success alert but I need to get files for foto_path as $_FILES not $_POST. My Question is why input files detected as $_POST["foto_path"] not as $_FILES["foto_path"]. How to change that to $_FILES["foto_path"] so I can start work with the files uploaded?

我获得了成功警报,但是我需要将foto_path的文件作为$_FILES而不是$_POST。我的问题是为什么输入文件检测为$_POST["foto_path"]而不是$_FILES["foto_path"]。如何将其更改为$_FILES["foto_path"],这样我就可以开始处理上传的文件了?

1 个解决方案

#1


5  

update : i solved this problem at last

更新:我最终解决了这个问题

var ft=$('#foto_path_upload')[0].files[0];
oMyForm.append("foto_path_upload", ft);

and this i got this from log :

这是我从日志中得到的:

09/07/2013 04:26:53 pm :: files -> Array
(
    [foto_path_upload] => Array
        (
           [name] => noimages.jpg
           [type] => image/jpeg
           [tmp_name] => D:\xampp\tmp\php4E90.tmp
           [error] => 0
           [size] => 3642
         )

 )    

Problem solved. thanx everyone

问题解决了。谢谢每个人

#1


5  

update : i solved this problem at last

更新:我最终解决了这个问题

var ft=$('#foto_path_upload')[0].files[0];
oMyForm.append("foto_path_upload", ft);

and this i got this from log :

这是我从日志中得到的:

09/07/2013 04:26:53 pm :: files -> Array
(
    [foto_path_upload] => Array
        (
           [name] => noimages.jpg
           [type] => image/jpeg
           [tmp_name] => D:\xampp\tmp\php4E90.tmp
           [error] => 0
           [size] => 3642
         )

 )    

Problem solved. thanx everyone

问题解决了。谢谢每个人