如何使用 Javascript 将多个文件上传到服务器
我正在使用 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
}
我想要上传多个文件,因此在成功回调中我想调用 uploadSingleFile 来移动到下一个文件。
如何存储我正在处理的文件?我正在使用 localStorage 来存储文件名。所以我想要这样做,
upload file localStorage.file0
upload file localStorage.file1
upload file localStorage.file2
所以我需要做的就是将数字存储在我们要达到的位置的末尾,0、1 等。我需要使用全局变量吗?看起来很乱。
如果我可以将数字作为附加参数传递给成功回调就好了?
I am using PhoneGap, and uploading a file (using a HTTP POST) like this,
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.
How can I store which file I am up to? I am using the localStorage to store the file names. 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. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯。这个问题值得怀疑吗?只需存储一个文件名数组,并使用 JSON.stringify / JSON.parse 进行数组和字符串之间的转换。
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.您可以将文件索引作为参数发送给 uploadSingleFile(),然后在 console.log() 中使用它
You can send the index of file as parameter to uploadSingleFile() then using it in console.log()
首先将所有图像添加到 array 中:
First add all your images to array :