Home > Uncategorized > Select and #Upload a file to #AWS S3 using #WinJS

Select and #Upload a file to #AWS S3 using #WinJS

winjslogo-purple-background

So, this is how to select a file from your phone, and upload it to Amazon S3, using the cordova plugin cordova-plugin-windows-filepicker  and some ASP.NET code on the server.

Here’s the WinJS (Javascript code)

window.plugins.WindowsFilePicker.open(function(uri) {
debugger;
var url = new Windows.Foundation.Uri(uri);
Windows.Storage.StorageFile.getFileFromApplicationUriAsync(url).then(function(file) {
debugger;
Windows.Storage.FileIO.readBufferAsync(file).done(function(buffer) {
debugger;
var bytes = new Uint8Array(buffer.length);
var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
dataReader.readBytes(bytes);
dataReader.close();
var base64 = btoa(String.fromCharCode.apply(null, bytes));
$http({
method: ‘POST’,
url: “http://www.yourserver.com/s3upload.aspx”,
headers: {
‘Content-Type’: ‘application/x-www-form-urlencoded’
},
transformRequest: function(obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + “=” + encodeURIComponent(obj[p]));
return str.join(“&”);
},
data: {
“data”: base64,
“extension”: file.fileType.substring(1)
}
}).then(function(response) {
$scope.file = response.data;
}, function(error) {
debugger;
});
});
}, function(error) {
debugger;
});
}, function(error) {
debugger;
});

This code is using AngularJS, but you can modify the AJAX part to use XHR or JQuery, as you need. – when complete it sets $scope.file to a URL hosted on Amazon AWS S3.

And here’s the code for the server-side:

var strBase64 = Request.Form[“data”];
var strExtension = Request.Form[“extension”];
var bBase64 = Convert.FromBase64String(strBase64);
var strUrl = S3.Upload(bBase64, strExtension);
Response.Write(strUrl);

Where the S3 class is as follows (username / password removed)

public static string Upload(byte[] data, string extension)
{
var ms = new MemoryStream(data);
var filename = Guid.NewGuid().ToString(“D”) + extension;
var cfg = new AmazonS3Config { RegionEndpoint = Amazon.RegionEndpoint.EUWest1 };
const string bucketName = “faxoff”;
var s3Client = new AmazonS3Client(“xxxxxx”, “yyyyy”, cfg);
var fileTransferUtility = new TransferUtility(s3Client);
var fileTransferUtilityRequest = new TransferUtilityUploadRequest
{
BucketName = bucketName,
InputStream = ms,
StorageClass = S3StorageClass.ReducedRedundancy,
Key = filename,
CannedACL = S3CannedACL.PublicRead
};
fileTransferUtility.Upload(fileTransferUtilityRequest);
return “https://s3-eu-west-1.amazonaws.com/xxxxx/” + filename;
}

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: