我有一个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文件。
请告知。
答案 0 :(得分:4)
令人尴尬但我在发布问题后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]");