开发者

What is the best way to upload multiple files in one request in Flash?

开发者 https://www.devze.com 2023-02-19 04:21 出处:网络
I\'m need to simulate a web form\'s ability to include as many File inputs as one wishes in the FORM and have all the files arrive together in one request.

I'm need to simulate a web form's ability to include as many File inputs as one wishes in the FORM and have all the files arrive together in one request.

What's the prescribed method for performing said operation in AS3? (I'm using AIR, but I doubt that makes any differe开发者_JAVA技巧nce).

TIA


You can do a multipart form upload. I think the class I used is at http://blog.inspirit.ru/?p=139, but I would have to double check when I get to the office.

EDIT: Yes, the link above is correct. This particular class makes file uploads and multipart/form-data pretty much a no brainer in AS3.


Like I said in the comment, I hate answering my own questions, but I found this solution: http://blog.mikestead.me/upload-multiple-files-with-a-single-request-in-flash/ and it works REALLY nicely and is entirely code complete, I had to do virtually no noodling at all. The only thing the docs fail to detail (which I'll add here) is that on the server side (specifically in PHP) the files arrive hash indexed by the variable name you set. So when you do

urlVariables["image1"] = new URLFileVariable(filePicker.data, filePicker.name); //this adds your file to the request in flash

Then on the PHP side you will find that file in

$_FILES["image1"]["tmp_name"];


You have two methods: zipping, or tracking multiple uploads.

If you want to zip the files, here is a walk-through on how to go about zipping, uploading, and unzipping from AIR.

If you want to upload multiple files, what you need to do is have an Array of booleans or a counter that keeps track of how many uploads were started. Then on an an event capturing upload completion, the booleans need to be turned from false to true or another counter needs to count the completions. Once all booleans are true or the two counters values match, you know all the files have been uploaded.

Here's some pseudo code:

fileCount= 0
fileUploadCompleteCount = 0

foreach(file in fileArray)
    fileCount++
    file.listenForUploadComplete(uploadComplete)

function uploadComplete()
    fileUploadCompleteCount++
    checkUploadProgress()

function checkUploadProgress()
    if(fileUploadCompleteCount == fileCount)
        // all files have been uploaded
0

精彩评论

暂无评论...
验证码 换一张
取 消