The RoLLing cOde

Rumor, crap and –un–original

Archive for November, 2007

File upload with URLRequest in AIR

without comments

A function to help upload without FileReference.upload() in AIR HTML/JS application.

Function Parameters:

void PrepareMultipartRequest(
 URLRequest request,
 ByteArray file_bytes,
  string field_name = "file",
  string native_path = "C:FILE",
  object data_before = {},
  object data_after = {}
);

Sample JS Code:

var request = new air.URLRequest('http://example.com/upload.php');
var loader = new air.URLLoader();
var file = new air.File('C:\TEST.TXT'); //use file.browseForOpen() on ur wish
var stream = new air.FileStream();
var buf = new air.ByteArray();
var extra = {
 "id": "abcd"
};
stream.open(file, air.FileMode.READ);
stream.readBytes(buf);
MultipartRequest(request, buf, 'myfile', file.nativePath, extra);
loader.load(request);

Sample PHP Code:

$id = $_POST['id'];
move_uploaded_file($_FILES['myfile']['tmp_name'], '/opt/blahblah');

Download:
multipart.js

Original idea inspired from
here
.

Written by freewizard

November 12th, 2007 at 8:51 pm

Posted in Crap

Tagged with