Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Ajax文件上传-脚本在文件中写入垃圾_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript Ajax文件上传-脚本在文件中写入垃圾

Javascript Ajax文件上传-脚本在文件中写入垃圾,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我的Ajax文件上传脚本有问题。 上载文件时,文件已损坏。当我用Notepad++打开文件时,我看到例如以下几行: -----------------------------22998260013704 内容配置:表单数据;name=“0”;filename=“myimage.png” 内容类型:图像/png 文件内容 -----------------------------22998260013704-- 当我删除filecontent后面的3行和filecontent后面的行时,文件就正常

我的Ajax文件上传脚本有问题。 上载文件时,文件已损坏。当我用Notepad++打开文件时,我看到例如以下几行:

-----------------------------22998260013704

内容配置:表单数据;name=“0”;filename=“myimage.png”

内容类型:图像/png

文件内容 -----------------------------22998260013704--

当我删除filecontent后面的3行和filecontent后面的行时,文件就正常了。 我不知道为什么这4行会写入文件。 我希望有人能帮助我

以下是我的Javascript代码:

var myFiles = [];
function ajaxFileUpload() {
    var dataid  = document.getElementById("dataid").getAttribute("data-id"),
        data    = new FormData(),
        maxSize = 100.0,
        file    = null, 
        myUrl   = "xxx/save";
    
$.each(myFiles, function(key, value) {
    console.log(key+" "+value);
    file = value;
    data.append(key, value);
});
var filesize = file.size;
if ((filesize/(1024*1024)) <= maxSize) {
    $.ajax({
        type: "PUT", //<-- http://stackoverflow.com/questions/10475313/ajax-file-upload-with-xmlhttprequest
        url: myUrl,
        processData: false,
        contentType: false,
        data: data,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("X-File-Name", file.name);
            xhr.setRequestHeader("X-File-Size", file.size);
            xhr.setRequestHeader("X-myid", dataid);
        },
        success: function (json) {
            //....
        },
    }); 
} else {
    //...
}
}
我发现了问题。 如果我直接将文件作为数据发送,而不是在FormData中发送,它就可以工作

因此,正确的代码是:

var myFiles = [];
function ajaxFileUpload() {
    var dataid  = document.getElementById("dataid").getAttribute("data-id"),
        maxSize = 100.0,
        file    = null, 
        myUrl   = "xxx/save";

$.each(myFiles, function(key, value) {
    file = value;
});
var filesize = file.size;
if ((filesize/(1024*1024)) <= maxSize) {
    $.ajax({
        type: "PUT", //<-- https://stackoverflow.com/questions/10475313/ajax-file-upload-with-xmlhttprequest
        url: myUrl,
        processData: false,
        contentType: false,
        data: file,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("X-File-Name", file.name);
            xhr.setRequestHeader("X-File-Size", file.size);
            xhr.setRequestHeader("X-myid", dataid);
        },
        success: function (json) {
            //....
        },
    }); 
} else {
    //...
}
}
var myFiles=[];
函数ajaxFileUpload(){
var dataid=document.getElementById(“dataid”).getAttribute(“数据id”),
最大尺寸=100.0,
file=null,
myUrl=“xxx/save”;
$.each(我的文件、函数(键、值){
文件=值;
});
var filesize=file.size;
如果((filesize/(1024*1024))我发现了问题。
如果我直接将文件作为数据发送,而不是在FormData中发送,它就可以工作

因此,正确的代码是:

var myFiles = [];
function ajaxFileUpload() {
    var dataid  = document.getElementById("dataid").getAttribute("data-id"),
        maxSize = 100.0,
        file    = null, 
        myUrl   = "xxx/save";

$.each(myFiles, function(key, value) {
    file = value;
});
var filesize = file.size;
if ((filesize/(1024*1024)) <= maxSize) {
    $.ajax({
        type: "PUT", //<-- https://stackoverflow.com/questions/10475313/ajax-file-upload-with-xmlhttprequest
        url: myUrl,
        processData: false,
        contentType: false,
        data: file,
        beforeSend: function(xhr) {
            xhr.setRequestHeader("X-File-Name", file.name);
            xhr.setRequestHeader("X-File-Size", file.size);
            xhr.setRequestHeader("X-myid", dataid);
        },
        success: function (json) {
            //....
        },
    }); 
} else {
    //...
}
}
var myFiles=[];
函数ajaxFileUpload(){
var dataid=document.getElementById(“dataid”).getAttribute(“数据id”),
最大尺寸=100.0,
file=null,
myUrl=“xxx/save”;
$.each(我的文件、函数(键、值){
文件=值;
});
var filesize=file.size;
如果((文件大小/(1024*1024))