我有一个ZIP文件,是在我的桌面Flex4.6应用程序的视图上使用拖放创建的。
这将触发一个将自动上载zip文件的服务。
我可以使用以下代码将有关zip文件的元数据发送到服务器。

        var urlRequest:URLRequest = new URLRequest(PUBLISH_ZIP_FILE_URL);
        // set to method=POST
        urlRequest.method = URLRequestMethod.POST;



        var params:URLVariables = new URLVariables();



        params['data[File][title]'] = 'Title1';
        params['data[File][description]'] = 'desc';
        // params['data[File][filename]'] =  I am not sure exactly what to use here
        // If this is a webpage, I expect to use input type="file" with the name as data[File][filename]


        urlRequest.data = params;

        addLoaderListeners();

        // set it such that data format is in variables
        loader.dataFormat = URLLoaderDataFormat.VARIABLES;

        loader.load(urlRequest);

我读过https://stackoverflow.com/questions/8837619/using-http-post-to-upload-a-file-to-a-website
然而,他们立即从bytearray开始,我根本不知道如何转换我的zip文件。
请告知。

最佳答案

很尴尬,但我在发帖42分钟后找到了答案。
有点像橡皮鸭的问题解决方法。
http://www.codinghorror.com/blog/2012/03/rubber-duck-problem-solving.html
简而言之:使用file类,特别是从upload类扩展而来的FileReference方法。
长答案:

        var urlRequest:URLRequest = new URLRequest(PUBLISH_ZIP_FILE_URL);
        // set to method=POST
        urlRequest.method = URLRequestMethod.POST;

        var params:URLVariables = new URLVariables();

        params['data[File][title]'] = 'Title1';
        params['data[File][description]'] = 'desc';

        // this is where we include those non file params and data
        urlRequest.data = params;


        // now we upload the file
        // this is how we set the form field expected for the file upload
        file.upload(urlRequest, "data[File][filename]");

09-17 17:38