我正在尝试创建可以将文件上传到已知FTP服务器的Chrome APP,但是当我尝试发送文件时,我的代码只会创建一个名称为0KB的文件
所有指导意见都得到了赞赏。
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length); // 2 bytes for each char
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
chrome.sockets.tcp.create( {persistent:false }, function(createInfo) {
socketID = createInfo.socketId;
chrome.sockets.tcp.connect( socketID, IP, PORT, onCallback);
});
function onCallback( result ){
console.log( "onCallback", result );
};
chrome.sockets.tcp.onReceive.addListener(function(info) {
var d = ab2str( info.data );
if (d.indexOf("220 ")>-1){
// send user.
console.log( "send " + ftp_username);
chrome.sockets.tcp.send( socketID, str2ab("USER "+ftp_username+"\n"), onCallback );
}
if (d.indexOf("331 ")>-1){
// send password.
console.log( "send password");
chrome.sockets.tcp.send( socketID, str2ab("PASS "+ftp_password+"\n"), onCallback );
}
if (d.indexOf("230 ")>-1){
chrome.sockets.tcp.send( socketID, str2ab("PASV\n"), function(e, outstream) {
chrome.sockets.tcp.send( socketID, str2ab("STOR icon.png\n"), onCallback );
} );
}
});