简体   繁体   English

如何使用JavaScript将多个文件上传到服务器

[英]How To Upload Multiple Files To Server Using Javascript

I am using PhoneGap, and uploading a file (using a HTTP POST) like this, 我正在使用PhoneGap,并像这样上传文件(使用HTTP POST),

function uploadSingleFile()
{
    var ft = new FileTransfer();
    // set up parameters etc
    ft.upload(imageName, "http://serviceaddress/UploadFile.ashx", win, fail, options);
}

function win(r)
{
    // success callback
}

I am wanting to upload muliple files, so in the success callback I want to call the uploadSingleFile to move onto the next file. 我想上传多个文件,因此在成功回调中,我想调用uploadSingleFile移至下一个文件。

How can I store which file I am up to? 如何存储我要处理的文件? I am using the localStorage to store the file names. 我正在使用localStorage来存储文件名。 So I would want to do this, 所以我想这样做

upload file localStorage.file0
upload file localStorage.file1
upload file localStorage.file2

So all I would need to do would be to store the number on the end, 0, 1, etc of where we are up to. 因此,我要做的就是将数字存储在最后的数字,0、1等处。 Do I need to use a global variable? 我需要使用全局变量吗? Seems messy. 看起来很乱。

If only I could pass through to the success callback a number as a additional parameter? 如果我只能传递给成功回调一个数字作为附加参数?

function uploadSingleFile(fileName) {

  var ft = new FileTransfer();

  ft.upload("fileUrl",
            "server",
            function (result , fileName) {
              console.log(fileName + ' has been uploaded successfully to server');                                          
            },
            function (error) {
              console.log(error);
            },
            {fileName: fileName, fileKey: "file"});
}

function uploadFiles() {
  var files = JSON.parse(localStorage.files);

  for(var i=0; i < files.length; i++) {
    uploadSingleFile(files[i]);
  }
}

You can send the index of file as parameter to uploadSingleFile() then using it in console.log() 您可以将文件的索引作为参数发送到uploadSingleFile(),然后在console.log()中使用它

Hmmm. Is the problem worth doubting? 问题值得怀疑吗? Just store an array of file names and use JSON.stringify / JSON.parse for conversion between array and string. 只需存储一个文件名数组,然后使用JSON.stringify / JSON.parse在数组和字符串之间进行转换即可。

First add all your images to array : 首先将所有图像添加到array中:

var TemplstImg = [];

function UploadImages()
{

    var lstImages = [localStorage.file0,localStorage.file1,localStorage.file2];
    TemplstImg=lstImages  ;

    if (TemplstImg.length > 0) {
        var img = TemplstImg.pop();
        uploadPhoto(img);
    }
}

function uploadPhoto(imageURI) {

    imageURI = imageURI.ImageFile;

    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
    options.mimeType = "image/jpeg";

    var params = {};
    params.value1 = "test";
    params.value2 = "param";

    options.params = params;

    var ft = new FileTransfer();

    ft.upload(imageURI, yourServerPath, winImg, failImg,options);
}

function winImg(r) {

    if (TemplstImg.length == 0) {
        alert ('Done , all files was uploaded'); 

    } else {
        var img = TemplstImg.pop();
        uploadPhoto(img);
    }
}

function failImg(error) {

    alert("failImg An error has occurred: Code = " + error.code);
    console.log("upload error source " + error.source);
    console.log("upload error target " + error.target);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM