2

这是我用于将图像上传到服务器的 HTTPClient 代码。这同样适用于 3.5.0.GA SDK 和 4.1.0.GA SDK,但不适用于新的 SDK 5.1.1.GA 和 5.1.2.GA。

var filename = "sample.png";
//Pointer to the file to be uploaded.
var uploadingFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);

Ti.API.info('uploadingFile ' + uploadingFile.exists());
//We are creating a new xhr instead of calling the existing one because we dont want timeout when uploading data.
var xhr = Titanium.Network.createHTTPClient({
    validatesSecureCertificate : false,
    enableKeepAlive : true,
    onload : function() {
        uploadingFile = null;
        Ti.API.info('Success '+tthis.responseText);         
    },
    onerror : function(data) {
        uploadingFile = null;
        Ti.API.info('this.status '+this.status);
    }
});

xhr.onsendstream = function(e) {
    var uploadTime = new Date();
    Ti.API.info('UPLOADING PROGRESS: ' + progress + ' ' + uploadTime.getHours() + ":" + uploadTime.getMinutes() + ":" + uploadTime.getSeconds());
};

xhr.open('POST', httpClient.getUserDomainInfo(config.URLs.imageupload, tenant));
xhr.send({
    file : uploadingFile.read(),
    claimId : claimID,
    filename : filename,
    description : ''
});

错误状态为 500 Internal Server Error。

是 SDK 中的问题还是我需要在代码中更改的任何内容。

请帮我。

4

2 回答 2

0

下面的代码演示了文件上传。根据你的代码修改。

f1 = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,<image name>);
i1 = f1.read();
xhr = Titanium.Network.createHTTPClient();
xhr.open('POST','<url to call>', false); // false makes it synchronous
xhr.onload = function() { handleAfterSentRouting(this.responseText);   };
xhr.send({media1: i1}); // media1 is the field the file information is in when you upload

谢谢。

于 2016-01-20T08:51:15.380 回答
0

几周后我遇到了同样的问题,解决方案非常简单,您只需要在服务器上创建另一个 Web 服务页面,仅用于文件上传而不传递任何其他文本参数,在您的情况下,编辑您的 xhr.send 参数以仅包含您的文件如下:

xhr.send({
    file : uploadingFile
}); 

因此,为您的其他变量创建另一个与另一个 Web 服务器页面相关的 xhr 函数:

claimId : claimID,
filename : filename,
description : ''

有用 ;)

于 2017-05-21T14:24:07.687 回答