New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload file to imgur #87

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

Comments

Projects
None yet
2 participants
@goldalworming

goldalworming commented Dec 29, 2013

Hi,

how to set upload url like imgur or another port?

@goldalworming

This comment has been minimized.

goldalworming commented Dec 29, 2013

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

@danialfarid

This comment has been minimized.

Owner

danialfarid commented Dec 29, 2013

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

This comment has been minimized.

Owner

danialfarid commented Dec 30, 2013

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

This comment has been minimized.

Owner

danialfarid commented Dec 30, 2013

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

This comment has been minimized.

goldalworming commented Dec 30, 2013

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