当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to upload image on google cloud storage using angualrjs

问题描述:

I want to upload an image on GCS using angular. Here is my code in which I am making a POST request to the endpoint that calls a database procedure to add user and return user_id if the user is added successfully then it hits webapp2 handler to upload its photo.

Angular Js code-

this.register = function (objImage) {

var route = URLS.UploadPhoto + '/uploadphoto';

var deferred = $q.defer();

deferred.resolve($http({

method:'POST',

url:route,

data : objImage

}).success(function (data) {

}));

return deferred.promise;

};

For image upload on front side I am using angularjs-imageupload-directive.

<input type="file" class="upload" accept="image/*" image="image"/>

and objImage is

objImage.imageFile = image.file;

Endpoint code

data = {'imageFile': request.imageFile}

result = urlfetch.fetch(url=UPLOAD_URL,payload=data,follow_redirects=False,deadline=30,method=urlfetch.POST,headers={})

request.imageFile is of messages.BytesField type.

webapp2 handler code

imageFile = self.request.get("imageFile")

fileName = '/' + Constants.BUCKET_NAME + Constants.PROFILE_PHOTO_FOLDER + '/' + 'mypphoto'

with gcs.open(filename, 'w', content_type = 'image/jpeg') as f:

f.write(imageFile)

f.close()

but this is showing error

File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/urlfetch.py", line 270, in fetch

allow_truncated, follow_redirects, validate_certificate)

File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/urlfetch.py", line 326, in make_fetch_call

request.set_payload(payload)

File "cpp_message.pyx", line 124, in cpp_message.SetScalarAccessors.Setter (third_party/apphosting/python/protobuf/proto1/cpp_message.cc:2229)

TypeError: <type 'dict'> has type <type 'dict'>, but expected one of: str, unicode

Am I missing something ? Or How can I do this ?

分享给朋友:
您可能感兴趣的文章:
随机阅读: