Download POST file using URLRequest

I have a quick question regarding loading POST files in ActionScript 3. I am trying to load ByteArray from memory through POST to a server. I use the URLRequest class to send data and URLLoader because I want to track progress. The following sections of code are listed:

var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php"); uploadRequest.method = URLRequestMethod.POST; uploadRequest.contentType = "multipart/form-data"; uploadRequest.data = myByteArray; var uploader:URLLoader = new URLLoader; uploader.addEventListener(ProgressEvent.PROGRESS, onUploadProgress); uploader.addEventListener(Event.COMPLETE, onUploadComplete); uploader.dataFormat = URLLoaderDataFormat.BINARY; uploader.load(uploadRequest); 

The problem is that I set my callbacks to track the progress of the download, and the bytesTotal property for the ProgressEvent is always 1960 (request size minus data?), Although the actual data is about 20 MB and no file is uploaded even after the full event is fired.

I have verified that upload.php works correctly with a simple html form, and I can verify that myByteArray contains all the data in question. Can someone tell me what I am doing wrong?

Edit:

I tried a couple of new things that I thought I should mention. The first sets the content type to application / octet-stream instead of multipart / form-data, which has no effect other than increasing the number of bytes until 1964. I also checked the Apache error log and found that the following text repeats a lot

PHP Warning: Missing border in POST data multipart / form-data in Unknown on line 0

I assume Flash is not formatting the HTTP request properly for any reason. Given that I created a FileReference that uses the same methods, I set URLLoader to load the file from disk and got the expected result: the bytesTotal property corresponded to the file size, and the file was loaded correctly.

Working with this, I found a page in the Adobe developer docs that mentions uploading data to the server using FileReference.upload () by setting the URLRequest data parameter, so I tried the following code:

 var uploadRequest:URLRequest = new URLRequest("http://127.0.0.1/upload.php"); uploadRequest.method = URLRequestMethod.POST; uploadRequest.data = myByteArray; fileRef = new FileReference; fileRef.addEventListener(ProgressEvent.PROGRESS, onUploadProgress); fileRef.addEventListener(Event.COMPLETE, onUploadComplete); fileRef.upload(uploadRequest); 

This led to the following conclusion:

ArgumentError: Error # 2127: POST data for FileReference cannot be a ByteArray type.

I am really stuck here. Any suggestions would be appreciated!

+6
source share
1 answer

You must add additional information to the "Content-Type" header:

 uploadRequest.contentType = "multipart/form-data; boundary=<<boundary here>>"; 

See an example here: http://marstonstudio.com/?p=36

+8
source

Source: https://habr.com/ru/post/909961/


All Articles