Javascript isn't uploading binary data

前端 未结 1 1860
天命终不由人
天命终不由人 2021-01-19 03:02

My javascript function only uploads text files correctly. Can anybody help me figure out how to make it also accept images etc. ?

function fileUpload(files)          


        
相关标签:
1条回答
  • 2021-01-19 03:37

    There is a W3C example of sending a GIF image using multipart/form-data at http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2:

    Content-Type: multipart/form-data; boundary=AaB03x
    
    --AaB03x
    Content-Disposition: form-data; name="submit-name"
    
    Larry
    --AaB03x
    Content-Disposition: form-data; name="files"
    Content-Type: multipart/mixed; boundary=BbC04y
    
    --BbC04y
    Content-Disposition: file; filename="file1.txt"
    Content-Type: text/plain
    
    ... contents of file1.txt ...
    --BbC04y
    Content-Disposition: file; filename="file2.gif"
    Content-Type: image/gif
    Content-Transfer-Encoding: binary
    
    ...contents of file2.gif...
    --BbC04y--
    --AaB03x--
    

    Notice the extra line Content-Transfer-Encoding: binary. Try adding that.

    EDIT: Try base64-encoding the file data using the Base64 jQuery plugin:

      var body = "--" + boundary + "\r\n";
      body += "Content-Disposition: form-data; name='upload'; filename='" + fileName + "'\r\n";
      body += "Content-Type: application/octet-stream\r\n";
      body += "Content-Transfer-Encoding: base64\r\n\r\n";
      body += $.base64Encode(fileData) + "\r\n";
      body += "--" + boundary + "--";
    
    0 讨论(0)
提交回复
热议问题