钛HTTPCLIENT可以't上载文件

Titanium HTTPCLIENT can't upload file

本文关键字:上载 文件 HTTPCLIENT 可以      更新时间:2023-12-12

我尝试使用浏览器可以上传文件,但如果我使用Titanium上传,它就不起作用,brower代码是浏览器上传文件:

<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

Upload.php文件:

$tmp_file=$_FILES['file']['tmp_name'];
$target_file=basename($_FILES['file']['name']);
move_uploaded_file($tmp_file, "files/".$target_file);

我的钛代码是:

var webaddress = 'http://' + address.value + ':' + port1.value + '/scanbatch/index.php';
xhr.open('POST',webaddress);
xhr.send({files : Titanium.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'scanbatch.txt')});

它可以工作,但web服务没有收到任何东西,只是收到了一个标头。

顺便说一句,我可以通过httpclient发送简短的xml,但如果xml更长,有时它无法发送,则失败的时间更长,我的意思是不总是,如果xml超过512KB,它总是失败。

我的代码是

var webaddress = 'http://' + address.value + ':' + port1.value + '/liveho/scanbatch';   
xhr.open('POST',webaddress);
xhr.send(xmlscript);

我的加载功能是

onload : function(e) {  
            alert(this.responseText);   
        },

请帮我谢谢

我会给你一些建议,以缩小问题范围(钛方面)。

你测试过文件的路径是否正确吗?

var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory + 
    Ti.Filesystem.getSeparator() + 'scanbatch.txt');
if(file.size)
    xhr.send({files : file});
else
    Ti.API.info("File " + file.nativePath + " doesn't exist or is empty");

如果路径不正确,请尝试在resourcesDirectory:中查找

var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory + 
    Ti.Filesystem.getSeparator() + 'scanbatch.txt');

断开连接的问题可能是减少设置超时属性。oneror函数也可以帮助您找到问题。onsendstream属性会让您知道上传的进度。

Ti.Network.createHTTPClient({
...
    onload : function(e) {  
        alert(this.responseText);   
    },
    onerror : function(e) {  
        Ti.API.debug(e.error);
        alert(e.error);  
    },
    onsendstream : function(e) {  
        var progress = parseFloat(e.progress)*100;
        Ti.API.info("Upload progress:" + progress + "%");
        //e.progress will contain a value from 0.0-1.0 with the progress of the upload
        alert("Upload progress:" + progress + "%"); 
    },
    timeout : 5000  // in milliseconds
...
});