2

我有一个 zip 文件,它是通过在我的桌面 Flex 4.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 文件。

请指教。

4

1 回答 1

4

令人尴尬,但我在发布问题 42 分钟后找到了答案。

这里正在解决一些橡皮鸭问题。

http://www.codinghorror.com/blog/2012/03/rubber-duck-problem-solving.html

Short answer: Use File class and specifically the method upload which is extended from the FileReference class.

Long answer:

        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]");
于 2012-05-27T09:07:56.743 回答