Upload file to imgur #87

Closed
goldalworming opened this Issue Dec 29, 2013 · 5 comments

Projects

None yet

2 participants

@goldalworming

Hi,

how to set upload url like imgur or another port?

@goldalworming

imgur return this error
{"data":{"error":"Noimagedatawassenttotheuploadapi","request":"/3/upload","method":"POST"},"success":false,"status":400}

@danialfarid
Owner

Seems like imgur accept file in application/json format not multipart form data. So $upload.upload() won't work there. You can use the ng-file-select directive to get the files and then use the FileReader api to read the file content similar to this:

var fileReader = new FileReader();
fileReader.readAsDataURL($files[i]);
(function uploadToImgur(fileReader, file) {
    fileReader.onload = function(e) {
        $http({method:'POST', url: ..., headers: { "Content-Type": file.type }, data: e.target.result});
    }
)(fileReader, $files[i]);

then send the binary data with an angular $post() to the imgur server like the example provided on their website.

The FileReader only works for HTML5 browsers though if you want cross-browser you can find a FileReader polyfill for those browsers.

@danialfarid
Owner

Just to give you an update. The FileReader api is gonna be supported in the next release of FileAPI around February 14. Once it is released I will integrate it with angular-file-upload so FileReader will be supported cross browser with the FileAPI polyfill.

@danialfarid
Owner

version 1.2.0 now supports this:

    var fileReader = new FileReader();
    fileReader.readAsArrayBuffer(file);
    fileReader.onload = function(e) {
        $upload.http({
            url: 'upload',
            headers: {'Content-Type': file.type},
            data: e.target.result
        }).then(function(response) {
            //success;
        }, null, function(evt) {
            $scope.progress[index] = parseInt(100.0 * evt.loaded / evt.total);
        });
    }

@goldalworming

wow..thanks a lot..you help me a lot..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment