Some of our latest projects used file uploading feature. Whether this is an excel, an audio or an image file, the file uploading mechanism remains the same. In a web application an user selects a file and uploads it to the server. Browser sends this file as a multipart-form file attachment, which is then handled on server.
The default HTML way to upload file to server is to use <input type="file"> element on a form. The rendering of such element is different in different browsers and looks rather ugly. Thus, almost all well known javascript libraries like JQuery, Kendo UI etc. provide their own implementations of file upload component. The key statement here is "almost", since in AngularJS bootstrap we didn't find anything like that. It worth to say that we've found several third-party implementations for file upload, but they either have rather complex implementation for such simple task or don't provide file selection feature. This is the reason why we've decided to implement this task by ourselves.
<input type="file">
Sources of our solution with upload-link directive and uiUploader service you may find here.
Their usage is rather simple. E.g. for upload-ink directive:
<a upload-link class="btn btn-primary" accept=".*" server-url="api/upload" on-success="controller.uploadSucceed(data, file)" on-error="controller.uploadFailed(e)">Click here to upload an image</a>
Where:
Usage of uiUploader service is also easy:
uiUploader.selectAndUploadFile("api/upload", ".jpg,.png,.gif"). then( function(result) { // TODO: handle the result. // result.data - contains the server response // result.file - contains uploaded File or Blob instance. }, $log.error);
In case when the first parameter is null the result.data contains the selected file content as a data URI.