简体   繁体   English

使用ajax php上传图像的问题

[英]issue with uploading image using ajax php

the ajax call for posting an image : 用于发布图像的ajax调用:

function FileUploadCheck() {
var formData = new FormData();
formData.append('LogoImageUploader', jQuery('#LogoImageUploader')[0].files[0]);

  jQuery.ajax({
          type: "POST",       
          url: "FileUploadChecker.php",
          contentType: "multipart/form-data",         
          data: formData,
          processData: false, 
          contentType: false,
          success : function(result){
              alert(result);

          }
        });
}

i assume the image is getting posted successfully 我认为图像已成功发布 请求后处理请求的有效负载

at the php script end : 在php脚本末尾:

<?php

echo var_dump($_FILES); 

?>

All of the above code works fine for me when i make use of jquery 1.7.2 but I can not keep using the jquery as other JS libraries existent in application as incompatible with the jquery 1.7.2 . 当我使用jquery 1.7.2时,上述所有代码对我来说都工作正常,但由于应用程序中存在与jQuery 1.7.2不兼容的其他JS库,我无法继续使用jquery。 I need to know if i could only import any standalone JS (maybe which provides ajax functionality) to make this work similar to jquery 1.7.2. 我需要知道是否只能导入任何独立的JS(也许提供ajax功能)以使其类似于jquery 1.7.2。

I found an answer to this : 我找到了答案:

function FileUploadCheck() {
var formData = new FormData();
formData.append('LogoImageUploader', jQuery('#LogoImageUploader')[0].files[0]);

    var http = new XMLHttpRequest();
    var url = "FileUploadChecker.php";

    http.open("POST", url, true);

    //when i uncommentated this line, the upload failed, as i tried to manually set the header to the post, but i let XHR to set the header automatically this works.   
    //http.setRequestHeader("Content-type", "multipart/form-data");



    http.onreadystatechange = function() {//Call a function when the state changes.
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(formData);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM