使用的FileReader API麻烦上传二进制文件麻烦、上传、二进制文件、FileReader

2023-09-10 20:06:29 作者:Ⅱ、暗恋是种很悲的调调

我使用FileReader.readAsBinaryString使用的multipart / form-data发布请求到服务器上传文件。

I'm using FileReader.readAsBinaryString to upload a file using a multipart/form-data POST request to a server.

该文件被发送,服务器接收并保存该文件。

The file gets sent and the server receives and saves the file.

当我试图打开我得到的消息说,它已损坏(PNG图像),或者我看到一个空白文档(PDF中的情况下)的服务器上的文件。显然,事情错了。

When I try to open the file on the server I get messages saying that it is corrupted (png images) or I see a blank document (in the case of a pdf). Obviously something is going wrong.

是否有需要被应用到在FileReader.onload处理程序返回在event.target.result的数据的其他编码?我失去了什么东西?

Is there some other encoding that needs to be applied to the data returned in event.target.result in the FileReader.onload handler? Am I missing something else?

感谢

推荐答案

尝试使用 FORMDATA ,而不是读文件作为二进制字符串和构建的multipart / form-data的手动请求。看到这里我的回应: HTML5文件API readAsBinaryString读取文件作为大得多,不是磁盘

Try using FormData instead of reading the file as a binary string and constructing the multipart/form-data request manually. See my response here: HTML5 File API readAsBinaryString reads files as much larger, different than files on disk