Adobe reference specifically points this out as a limitation.
Here's the note in the file.upload functionality in adobe reference.
Note: If your server requires user authentication, only SWF files running in a browser — that is, using the browser plug-in or ActiveX control — can provide a dialog box to prompt the user for a username and password for authentication, and only for downloads. For uploads using the plug-in or ActiveX control, or for uploads and downloads using the stand-alone or external player, the file transfer fails.
click here for the full reference page for File
Has anyone been able to work around this limitation. One thought I had was to use the native support in AIR for Javascript and see that works. Anyone tried that? Anyone have other ideas?
I've also tried the solution proposed here and it didn't work. According to the questioner in the forum it didn't seem to work for him either. Probably hitting the issue/limitation mentioned above.
I understand how to include basic authentication on a URLRequest and that works fine for me if I'm posting key/value pairs. But when I attempt to upload a file it does not work. It just sits there and does not fire any of the file.upload events. No errors no progress. Any help is much appreciated.
Here's my sampl开发者_JAVA百科e code:
var sendVars:URLVariables = new URLVariables();
sendVars.prop1 = "filename" ;
var urlRequest:URLRequest = new URLRequest();
urlRequest.method = URLRequestMethod.POST ;
urlRequest.data = sendVars ;
var encoder:Base64Encoder = new Base64Encoder();
encoder.encode("user:pass");
var credsHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + encoder.toString());
urlRequest.requestHeaders.push(credsHeader);
file = new File(url);
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA , file_UploadCompleteDataHandler );
file.addEventListener( Event.OPEN, fileOpenHandler);
file.addEventListener(ProgressEvent.PROGRESS, fileProgressHandler);
file.addEventListener(Event.COMPLETE, file_CompleteHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, file_IOErrorHandler);
file.addEventListener(HTTPStatusEvent.HTTP_STATUS , file_HTTPStatusHandler);
file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, file_SecurityErrorHandler);
try {
// Start the file upload
file.upload(urlRequest, "primaryFile", false);
}
catch (error:Error) {
trace( "Unable to upload file." + file.name);
}
Do exactly what you are doing except get the file.data and pass it into this....
public static function prepareWithFormData(vars:MetadataList, bytes:ByteArray, fieldName:String, filename:String, request:URLRequest):void {
bytes.position = 0;
var boundary: String = '---------------------------' + UIDUtil.createUID();
var header1: String = "";
var varsArray:Array = vars.asArray();
for each(var item:Metadata in varsArray) {
header1 += "\r\n--" + boundary + "\r\n";
header1 += 'Content-Disposition: form-data; name="' + item.name + '"\r\n\r\n';
header1 += item.value;
}
header1 += '\r\n--'+boundary + '\r\n'
+'Content-Disposition: form-data; name="' + fieldName + '"; filename="'+filename+'"\r\n'
+'Content-Type: application/octet-stream\r\n\r\n'
//In a normal POST header, you'd find the image data here
var header2:String = '\r\n--'+boundary + '--';
//Encoding the two string parts of the header
var headerBytes1: ByteArray = new ByteArray();
headerBytes1.writeMultiByte(header1, "ascii");
var headerBytes2: ByteArray = new ByteArray();
headerBytes2.writeMultiByte(header2, "ascii");
//Creating one final ByteArray
var sendBytes: ByteArray = new ByteArray();
sendBytes.writeBytes(headerBytes1, 0, headerBytes1.length);
sendBytes.writeBytes(bytes, 0, bytes.length);
sendBytes.writeBytes(headerBytes2, 0, headerBytes2.length);
request.data = sendBytes;
request.method = URLRequestMethod.POST;
request.contentType = "multipart/form-data; boundary=" + boundary;
}
精彩评论