Apache flex Adobe AIR/Flex文件上载到需要基本身份验证的服务器
AdobeReference特别指出这是一个限制 以下是adobe参考中file.upload功能中的注释 注意:如果服务器需要用户身份验证,则只有在浏览器中运行的SWF文件(即使用浏览器插件或ActiveX控件)才能提供对话框,提示用户输入用户名和密码进行身份验证,并且仅用于下载。对于使用插件或ActiveX控件的上载,或者对于使用单机或外部播放器的上载和下载,文件传输失败 有没有人能够绕过这个限制。我的一个想法是使用AIR中对Javascript的本机支持,然后看看它是否有效。有人试过吗?有人有其他想法吗 我也尝试过提出的解决方案,但没有成功。据论坛上的提问者称,这似乎对他也不起作用。可能触及上述问题/限制 我了解如何在URL请求中包含基本身份验证,如果我发布密钥/值对,这对我来说很好。但当我试图上传一个文件时,它不起作用。它只是放在那里,不触发任何file.upload事件。没有错误就没有进步。非常感谢您的帮助 以下是我的示例代码:Apache flex Adobe AIR/Flex文件上载到需要基本身份验证的服务器,apache-flex,upload,air,basic-authentication,Apache Flex,Upload,Air,Basic Authentication,AdobeReference特别指出这是一个限制 以下是adobe参考中file.upload功能中的注释 注意:如果服务器需要用户身份验证,则只有在浏览器中运行的SWF文件(即使用浏览器插件或ActiveX控件)才能提供对话框,提示用户输入用户名和密码进行身份验证,并且仅用于下载。对于使用插件或ActiveX控件的上载,或者对于使用单机或外部播放器的上载和下载,文件传输失败 有没有人能够绕过这个限制。我的一个想法是使用AIR中对Javascript的本机支持,然后看看它是否有效。有人试过吗?
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);
}
除了获取file.data并将其传递给此…之外,请执行您正在执行的操作
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;
}
你是说我应该像对待URLRequest那样对待它,而不是使用file.upload。我应该这样做:对不起,在我处理完它之前,最后一条评论被打断了:你是说我应该像对待URLRequest那样对待它,而不是使用file.upload。我应该像你建议的那样使用URLRequest并填充它的.data在这里如果不是通过file.upload,我最终如何发送它。我应该使用什么装载机?