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

发布于 2024-09-28 00:04:04 字数 2147 浏览 7 评论 0原文

Adobe 参考资料特别指出这是一个限制。

这是 adobe 参考中 file.upload 功能的注释。

注意:如果您的服务器需要用户身份验证,则只有在浏览器中运行的 SWF 文件(即使用浏览器插件或 ActiveX 控件)才能提供对话框来提示用户输入用户名和密码进行身份验证,并且仅适用于下载。使用插件或ActiveX控件上传,或者使用单机或外部播放器上传和下载,文件传输失败。

单击此处获取完整参考文件页面

有没有人能够解决此限制。我的一个想法是使用 AIR for Javascript 中的本机支持并看看是否有效。有人试过吗?有人还有其他想法吗?

我还尝试了此处提出的解决方案,但它没有不工作。根据论坛提问者的说法,这似乎对他也不起作用。可能遇到了上面提到的问题/限制。

我了解如何在 URLRequest 上包含基本身份验证,如果我发布键/值对,这对我来说效果很好。但是当我尝试上传文件时它不起作用。它只是坐在那里,不会触发任何 file.upload 事件。没有错误就没有进步。非常感谢任何帮助。

这是我的示例代码:

            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);
        }

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 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);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2024-10-05 00:04:04

做你正在做的事情,除了获取 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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文