Appcelerator - 分段发送大视频

Appcelerator - Send large video in parts

我想通过我的应用程序发送大型视频。我的想法是分段发送视频,因为如果连接断开,我可以继续发送剩余的部分。我正在处理 300MB 的视频。

选项 1

我可以使用 Ti.BLOB 个对象吗?在 Android 中,我在将 Blob 对象用于视频时遇到了问题。 -> 我试过了,出现了这条消息:

*** ASL MESSAGE SIZE (8653314 bytes) EXCEEDED MAXIMIMUM SIZE (2048000 bytes) ***

所以这个选项不起作用。

选项 2

我找到了一个用于 appcelerator 的 zip/unzip 模块,但我无法将 zip 分成几个部分。

最好的方法是什么?有什么想法吗?

谢谢。

使用套接字在 Titanium 中尝试 FTP 二进制数据文件上传器

https://gist.github.com/abada/d762f2e1151903505be9

// FTP binary files via FTP using Sockets. 

var ftp_host = '1.2.3.4'; // the IP address of your FTP server
var ftp_port = 21;
var ftp_username = 'username';
var ftp_password = 'yourftppassword';
var ftp_home = '/home/folder'; // Your FTP home folder. Or where ever you want to 'CWD' to.

var socket_control;   // main control socket
var socket_data;      // for read/write
var open_data = false;


function connectFTP(){
  Ti.API.info('Connecting FTP Socket.. ');
  var socket = Ti.Network.Socket.createTCP({
      host: ftp_host, 
      port: ftp_port,
      connected: function (e) {
          Ti.API.info('Socket opened!');
          socket_control = e.socket;
          Ti.Stream.pump(e.socket, readCallback, 1024, true);

      },
          error: function (e) {
          Ti.API.info('Error (' + e.errorCode + '): ' + e.error);
      },
  });
  socket.connect();

}

function readCallback(e) {
    if (e.bytesProcessed == -1)
    {
        // Error / EOF on socket. Do any cleanup here.
        console.log('readCallback error');
    }
    try {
        if(e.buffer) {
            var responseCode = e.buffer.toString().substr(0,3);
            // http://en.wikipedia.org/wiki/List_of_FTP_server_return_codes

            var received = e.buffer.toString();
            Ti.API.info('Received: ' + received);
            Ti.API.info('Status: ' + responseCode);
            // Ti.API.info(e);
            switch(responseCode) {
              case "220":
                Ti.API.info("FTP: Logging in");
                Ti.Stream.write(socket_control, Ti.createBuffer({
                  value: 'USER ' + ftp_username + '\n'
                }), function(_e){
                  // console.log('callback after USER...');
                  // console.log(_e);
                });
                break;
              case "331":
                Ti.API.info("FTP: Sending credentials");
                Ti.Stream.write(socket_control, Ti.createBuffer({
                  value: 'PASS ' + ftp_password + '\n'
                }), function(_e){
                  // console.log('callback after PASS...');
                  // console.log(_e);
                });
                break;
              case "230":
                Ti.API.info("FTP: Log in successful");
                Ti.Stream.write(socket_control, Ti.createBuffer({
                  value: 'CWD ' + ftp_home + '\n'
                }), function(_e){
                  // console.log('callback after login CWD ...');
                  // console.log(_e);
                });
                break;
              case "225":
                Ti.API.info("FTP: Data Connection Opened");

                break;
              case "226":
                Ti.API.info("FTP: Closing Data Connection");

                break;        
              case "257":
                Ti.API.info("FTP: Pathname created...");

                break;
              case "221":
                Ti.API.info("FTP: Disconnected");
                break;
              case "227":
                Ti.API.info("FTP: Passive mode entered : open_data:"+open_data);

                if(open_data){ // Trap this when we want it and not after other 'PASV' commands
                  console.log('preflight STOR PASV response: ');
                  // Parse the response.. 
                  var msg = e.buffer.toString();
                  msg = msg.split('(')[1].split(')')[0].split(',');
                  if(msg.length==6){
                    // looks ok.. 
                    var m6 = msg.pop()*1; // forces int
                    var m5 = msg.pop()*1;
                    var remote_ip = msg.join('.');
                    console.log('remote ip : '+remote_ip);
                    var remote_port = Math.round((m5 * 256) + m6); // Magic port calculation! 
                    console.log('remote port : '+remote_port);

                    // OK.. now set up the data socket.. 
                    var socket = Ti.Network.Socket.createTCP({
                        host: remote_ip, 
                        port: remote_port,
                        connected: function (e) {
                            Ti.API.info('DATA Socket opened!');
                            // saving this 
                            socket_data = e.socket;
                            Ti.Stream.pump(e.socket, readDataCallback, 1024, true);
                            // set binary transfer mode TYPE I
                            Ti.Stream.write(socket_control, Ti.createBuffer({
                              value: 'TYPE I\n'
                            }), function(_e){
                              console.log('callback after setting TYPE I ...');
                              console.log(_e);
                              if(_e.success){
                                  // Binary transfer mode is now ready
                                  // Tell the server the name of the file
                                  // this does not have to be the actual name of the local file
                                  var remote_filename = 'test.jpg';
                                  console.log('send STOR '+remote_filename+' to control socket...');
                                  // now send the STOR to the control socket.. hopefully get a 150 back...
                                  Ti.Stream.write(socket_control, Ti.createBuffer({
                                    value: 'STOR '+remote_filename+'\n'
                                  }), function(_e){
                                    //console.log('callback after pre-STOR PASV ...');
                                    //console.log(_e);
                                  });
                              }
                            });                            
                        },
                            error: function (e) {
                            Ti.API.info('Error (' + e.errorCode + '): ' + e.error);
                        },
                    });
                    console.log('connecting DATA socket');
                    socket.connect();
                  }
                }
                break;
              case "150":

                console.log('DATA connection initiated on control socket!... writing file contents to socket_data');
                // by default the data connection is in ASCII mode. 
                // so, we called 'TYPE I' beforehand to set it to BINARY mode.
                // Now get the file we want to send... (from wherever is is). Need to be a
                var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'test.jpg');
                var file_data = file.read(); // verbose for clarity
                var file_buffer = Ti.createBuffer(); // create empty buffer
                // Create a file stream object with the file_data
                var file_stream = Ti.Stream.createStream({ source : file_data, mode : Titanium.Stream.MODE_READ});
                // Add it to the buffer
                var content_size = file_buffer.append(Ti.Stream.readAll(file_stream));
                console.log('File Size : ' + content_size ); // append returns the bytes appended

                console.log('WRITE FILE to socket now .. ');
                // write to socket_data :
                Ti.Stream.write(socket_data, file_buffer, function(_e){
                  console.log('callback after socket_data write.. ');
                  console.log(_e);
                  if(_e.success){
                    // WOO HOOOOOO!!!!!! It worked!! 
                    console.log('FTP : file uploaded OK!  closing connection..');
                    socket_data.close();
                    // Nullify stuff
                    file_buffer = null;
                    file_stream = null;
                    file_data = null;
                    file = null;

                    open_data = false; // Our PASV trap

                  } else {
                    console.log('FTP ERROR ');
                  }
                });
                break;
              case "250":
                Ti.API.info("FTP: Changed active directory to " + ftp_home);
                Ti.Stream.write(socket_control, Ti.createBuffer({
                  value: 'PASV\n'
                }), function(_e){
                  console.log('callback after PASV ...');
                  console.log(_e);
                });
                break;
              case "530":
                Ti.API.info("FTP: Login failed, disconnecting");
                break;
              default:
                Ti.API.info("FTP: " + e.buffer.toString());
                break;
            }

        } else {
            Ti.API.error('Error: read callback called with no buffer!');
        }
    } catch (ex) {
        Ti.API.error(ex);
    }
}

function readDataCallback(e) {
    if (e.bytesProcessed == -1)
    {
        // Error / EOF on socket. Do any cleanup here.
        console.log('readDataCallback error');
    }
    try {
        if(e.buffer) {
            var responseCode = e.buffer.toString().substr(0,3);

            var received = e.buffer.toString();
            Ti.API.info('DATA Received: ' + received);
            Ti.API.info('DATA Status: ' + responseCode);
            Ti.API.info(e);

        } else {
            Ti.API.error('Error: readDataCallback called with no buffer!');
        }
    } catch (ex) {
        Ti.API.error(ex);
    }
}

// Set up some UI and buttons 

var win = Ti.UI.createWindow({ backgroundColor: "#fff" });
var buttonConnect = Ti.UI.createButton({ title: "Connect", width: 280, height: 40, top: 40, left: 20 });
var buttonWrite = Ti.UI.createButton({ title: "Write File", width: 280, height: 40, top: 100, left: 20 });
var buttonDisconnect = Ti.UI.createButton({ title: "Disconnect", width: 280, height: 40, top: 160, left: 20 });

buttonConnect.addEventListener("click", function(_event) {
  // Set up connection
    connectFTP();
});

buttonWrite.addEventListener("click", function(_event) {

  open_data = true;// will open second datasocket after the PASV before the STOR 
  // Send the initial PASV command, so we can get the data host:port to use
  Ti.Stream.write(socket_control, Ti.createBuffer({
    value: 'PASV\n'
  }), function(_e){
    //console.log(_e);
  });


});

buttonDisconnect.addEventListener("click", function(_event) {



});

win.add(buttonConnect);
win.add(buttonWrite);
win.add(buttonDisconnect);
win.open();

使用Ti.Buffer和Ti.Stream分块上传。我在这里写了博客:

http://lowb1rd.github.io/002-chunked-uploads-with-titanium.html