简体   繁体   English

将Adobe AIR / Flex文件上传到需要基本身份验证的服务器

[英]Adobe AIR/Flex file upload to a server that requires basic authentication

Adobe reference specifically points this out as a limitation. Adobe参考特别指出了这一限制。

Here's the note in the file.upload functionality in adobe reference. 这是Adobe参考中file.upload功能的注释。

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. 注意:如果服务器需要用户身份验证,则只有在浏览器中运行的SWF文件(即使用浏览器插件或ActiveX控件)可以提供一个对话框,提示用户输入用户名和密码进行身份验证,并且仅针对下载。 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. 对于使用插件或ActiveX控件的上载,或者对于使用独立播放器或外部播放器的上载和下载,文件传输失败。

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. 我曾经想到的是使用AIR for Javascript的本机支持,然后看看能行得通。 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. 我了解如何在URLRequest上包括基本身份验证,如果我要发布键/值对,那对我来说效果很好。 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. 它只是坐在那里,不会触发任何file.upload事件。 No errors no progress. 没有错误就没有进步。 Any help is much appreciated. 任何帮助深表感谢。

Here's my sample 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.... 除了获取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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM