如果在iOS和放大器的web应用程序使用FORMDATA发送的文件转换为[Object对象]串; Android版放大器、转换为、应用程序、对象

2023-09-06 10:30:04 作者:沵若成风

我建立基于科尔多瓦/ PhoneGap的使用的multipart / form-data发布请求,上传文件到服务器的Web应用程序。

I am building a web app based on Cordova/PhoneGap which uploads files to a server using a multipart/form-data POST request.

以下code适用于黑莓10:

The following code works on BlackBerry 10:

var postRequest = new XMLHttpRequest();
if (postRequest.overrideMimeType) {
    postRequest.overrideMimeType('text/xml');
}
var fd = new FormData();
fd.append('param1', 'value1');
[...]
fd.append('file', file, fileName);
postRequest.open('POST', url, true);
postRequest.send(fd);

不过在iPhone上运行iOS6的的文件对象由字符串替换 [Object对象当发送到服务器,类似于在Safari转换文件设定为[对象对象]当插入FORMDATA。如何解决?。

However on an iPhone running iOS6 the File object is replaced by the String [Object object] when sent to the server, similar to the issue described in Safari converts File to [object Object] when inserted into FormData. How to fix?.

在对比的是问题,我不是克隆的File对象。我的应用程序调用通过打开的...菜单从其他应用程序的文件。我通过一个本地文件系统的URI,我解决您在使用 window.resolveLocalFileSystemURI()。这工作完全和我收到的第一一FileEntry的,并从一个File对象。然而,它显示该对象未正确识别和传送该请求到服务器时,其的toString()方法被调用,而不是

In contrast to that issue I am not cloning the File object. My app is invoked with documents from other apps via the "Open in..." menu. I am passed a local file system URI which I resolve using window.resolveLocalFileSystemURI(). This works perfectly and I receive first a FileEntry and from that a File object. However it appears this object is not recognized correctly and its toString() method is called instead when transmitting the request to the server.

这是iOS上的错误吗?或者,也许在PhoneGap的一个bug创建文件对象时?什么是最好的解决办法?

Is this a bug on iOS? Or maybe a bug in PhoneGap when creating the File object? What is the best workaround?

更新:我只是碰到了Android上的同样的问题,因此,此问题似乎并没有具体到iOS

UPDATE: I just ran into the same problem on Android, so this problem doesn't seem to be specific to iOS.

推荐答案

适用于iOS可能的解决方法似乎是读取使用的FileReader文件,从它构造一个新的Blob对象,然后将本到服务器,而不是:

A possible workaround for iOS seems to be to read the file using FileReader, construct a new Blob object from it and then transmit this to the server instead:

var reader = new FileReader();
reader.onloadend = function(evt) {
     var fileBlob = new Blob([evt.target.result], { 'type' : fileType });
};
reader.readAsArrayBuffer(file);

这里所使用的一滴构造需要iOS 6.0或更高版本。通过这种方式,数据被完全发送到作为预定的服务器

The Blob constructor used here requires iOS 6.0 or higher. This way the data was completely transmitted to the server as intended.

一个斑点可以在Android被创建,也使用WebKitBlobBuilder。不幸的是这并没有在我的测试解决Android上的问题。

A Blob can be created on Android, too, using WebKitBlobBuilder. Unfortunately this didn't fix the problem on Android during my tests.