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

时间:2015-06-15 13:03:05

标签: javascript php jquery ajax

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

  

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

     

内容 - 处置:表单数据;名称=" 0&#34 ;;文件名=" myimage.png"

     

内容类型:image / png

     

filecontent

     

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

当我删除3行bevor filecontent和filecontent之后的Line时,文件正常。 我不知道为什么这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 {
    //...
}
}

这是我的相关PHP代码:

 private function copyPutFilesToTmp($directory = "") {
    $temp = "xxx";
    if (!is_dir($temp)) {
        mkdir ($temp, 0777, true);
    }
    $tmpPath            = $temp."/";
    $filename           = $_SERVER['HTTP_X_FILE_NAME'];
    $in                 = fopen('php://input', 'r');
    $ziel               = $tmpPath.$filename;
    if (!file_exists($ziel)) {
            $fileuploadok   = true;
            $out            = fopen($ziel, 'w');
            $data       = fread($in, 1024);
            while($data) {
                if ($data != false) {
                    fwrite($out, $data);                        
                } else {
                    $fileuploadok = false;
                }
                $data       = fread($in, 1024);
            }
            fclose($in);
            fclose($out);
            if ($fileuploadok === FALSE) {
                //...
            } else {
                //...
            }
        } else {
            //...
    }
    return $answer;
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。 如果我直接将文件作为数据发送而不是在FormData中发送它就可以了!

所以正确的代码是:

dt1
    # SID Chromo PP P1 P2 P3
 # 1:  S1      1  1  0  0  0
 # 2:  S2      1  2  0  0  0
 # 3:  S3      1  3  0  0  0
 # 4:  S4      1  4  0  0  0
 # 5:  S5      1  5  0  0  0
 # 6:  S6      2  1  0  0  0
 # 7:  S7      2  2  0  0  0
 # 8:  S8      2  3  0  0  0
 # 9:  S9      2  4  0  0  0
# 10: S10      2  5  0  0  0
# 11: S11      3  1  0  0  0
# 12: S12      3  2  0  0  0
# 13: S13      3  3  0  0  0
# 14: S14      3  4  0  0  0
# 15: S15      3  5  0  0  0

dt2
  # PID Chr start end         val
 # 1:  P2   1     1   2 -0.23298702
 # 2:  P1   1     1   3 -1.01500872
 # 3:  P2   1     4   5 -0.81726793
 # 4:  P3   2     2   3  0.25523700
 # 5:  P2   2     2   4  0.77209084
 # 6:  P3   2     4   5  0.36658112
 # 7:  P2   3     1   1 -0.16561194
 # 8:  P1   3     1   4 -0.07963674
 # 9:  P2   3     2   3  0.97287443
# 10:  P3   3     2   5  1.18078924
# 11:  P2   3     4   5  1.71653398

df1
   # SID Chromo PP          P1         P2        P3
# 1   S1      1  1 -1.01500872 -0.2329870 0.0000000
# 2   S2      1  2 -1.01500872 -0.2329870 0.0000000
# 3   S3      1  3 -1.01500872  0.0000000 0.0000000
# 4   S4      1  4  0.00000000 -0.8172679 0.0000000
# 5   S5      1  5  0.00000000 -0.8172679 0.0000000
# 6   S6      2  1  0.00000000  0.0000000 0.0000000
# 7   S7      2  2  0.00000000  0.7720908 0.2552370
# 8   S8      2  3  0.00000000  0.7720908 0.2552370
# 9   S9      2  4  0.00000000  0.7720908 0.3665811
# 10 S10      2  5  0.00000000  0.0000000 0.3665811
# 11 S11      3  1 -0.07963674 -0.1656119 0.0000000
# 12 S12      3  2 -0.07963674  0.9728744 1.1807892
# 13 S13      3  3 -0.07963674  0.9728744 1.1807892
# 14 S14      3  4 -0.07963674  1.7165340 1.1807892
# 15 S15      3  5  0.00000000  1.7165340 1.1807892  

在此处找到:AJAX File Upload with XMLHttpRequest