The RoLLing cOde

Rumor, crap and unoriginal | 谣言、扯淡、非原创

2007-11-12

File upload with URLRequest in AIR

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 code inspired from here.

Labels: ,

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home