我有一个 zip 文件,该文件是通过在桌面 Flex 4.6 应用程序中的 View 上拖放而创建的。
这会触发自动上传 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]");
关于actionscript-3 - 通过 Actionscript 3.0 使用 HTTP POST 上传 zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10772616/