react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Upload a picture to a server

Open jnak opened this issue 11 years ago • 35 comments

Hey,

I'm trying to upload to a server a picture from the Camera Roll.

I can access an image representation that looks like this:

{
"node": {
  "type":"ALAssetTypePhoto",
  "group_name":"Camera Roll",
  "location":{},
  "image" :{
    "isStored":true,
     "width":1280,
    "uri":"assets-library://asset/asset.JPG?id=AC072879-DA36-4A56-8A04-4D467C878877&ext=JPG",
    "height":850},
    "timestamp":1405312098
  }
}

I was thinking of (somehow) read the binary file, convert that to base64 and post this as JSON using fetch. Is that the best way to upload this to a server for now ?

jnak avatar Mar 26 '15 00:03 jnak

FYI transferring data back and forth between JS and Obj-C is relatively slow so you'll probably want to keep all of the image data in Obj-C. I don't have numbers but I expect you will see it takes a measurable amount of time to transfer several MB of data. It's small compared to the amount of time it takes to upload the photo but it's a good idea to keep the JS <=> Obj-C message queue clear.

You could write a tiny bridge module that receives the asset URL and then performs the upload, reporting progress/error/success information back to JS.

ide avatar Mar 26 '15 00:03 ide

You can do

xhr.send(node.image)

It's going to send it as an attachment. We use that in the ads manager app

(I hope it's in the open source version)

vjeux avatar Mar 26 '15 00:03 vjeux

@ide I know it s not awesome. But I just need to upload one picture for now. I just want a quick way to get it done. I will optimize later :)

@vjeux it seems that the request is empty. When I pass in a string though, I do get the params passed as url params.

What's my best move to get that pic on a server while waiting for xhr.send to be updated ?

Thx!

jnak avatar Mar 26 '15 02:03 jnak

cc @nicklockwood do you know if there's a difference between network implementation on opensource/internal?

vjeux avatar Mar 26 '15 02:03 vjeux

@vjeux yes, they're separate. I don't think we support image upload in OSS at the moment.

nicklockwood avatar Mar 26 '15 02:03 nicklockwood

@nicklockwood do you know how I can read the binary data of the image in JS ? It's dirty but it will do the job :)

Thx guys!

jnak avatar Mar 26 '15 03:03 jnak

FYI transferring data back and forth between JS and Obj-C is relatively slow

I've found that React Native's bridge impl seems to be slower that cordova's :(

I don't have specific bench marks, but if the queue is rapidly utilized, the UI becomes non-responsive whereas with a cordova app, the UI works fine.

I understand the mechanics of why a RN app would be come unresponsive, but I just had to add my experience.

JG

:: sent from my mobile device ::

On Mar 25, 2015, at 20:47, James Ide [email protected] wrote:

FYI transferring data back and forth between JS and Obj-C is relatively slow so you'll probably want to keep all of the image data in Obj-C. I don't have numbers but I expect you will see it takes a measurable amount of time to transfer several MB of data. It's a small compared to the amount of time it takes to upload the photo but it's a good idea to keep the JS <=> Obj-C message queue clear.

You could write a tiny bridge module that receives the asset URL and then performs the upload, reporting progress/error/success information back to JS.

— Reply to this email directly or view it on GitHub.

jaygarcia avatar Mar 27 '15 00:03 jaygarcia

I d be down implementing this in objective c, but since I know you guys are about to release the internal fb component, I d rather spend my time doing something else :) On Thu, Mar 26, 2015 at 8:43 PM Jay Garcia [email protected] wrote:

FYI transferring data back and forth between JS and Obj-C is relatively slow

I've found that React Native's bridge impl seems to be slower that cordova's :(

I don't have specific bench marks, but if the queue is rapidly utilized, the UI becomes non-responsive whereas with a cordova app, the UI works fine.

I understand the mechanics of why a RN app would be come unresponsive, but I just had to add my experience.

JG

:: sent from my mobile device ::

On Mar 25, 2015, at 20:47, James Ide [email protected] wrote:

FYI transferring data back and forth between JS and Obj-C is relatively slow so you'll probably want to keep all of the image data in Obj-C. I don't have numbers but I expect you will see it takes a measurable amount of time to transfer several MB of data. It's a small compared to the amount of time it takes to upload the photo but it's a good idea to keep the JS <=> Obj-C message queue clear.

You could write a tiny bridge module that receives the asset URL and then performs the upload, reporting progress/error/success information back to JS.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub https://github.com/facebook/react-native/issues/201#issuecomment-86773682 .

jnak avatar Mar 27 '15 01:03 jnak

It might actually be awhile (or never) before we release the networking module we use internally, so I wouldn't wait for that if you need the functionality...

On Mar 26, 2015, at 6:02 PM, Julien Nakache [email protected] wrote:

I d be down implementing this in objective c, but since I know you guys are about to release the internal fb component, I d rather spend my time doing something else :) On Thu, Mar 26, 2015 at 8:43 PM Jay Garcia [email protected] wrote:

FYI transferring data back and forth between JS and Obj-C is relatively slow

I've found that React Native's bridge impl seems to be slower that cordova's :(

I don't have specific bench marks, but if the queue is rapidly utilized, the UI becomes non-responsive whereas with a cordova app, the UI works fine.

I understand the mechanics of why a RN app would be come unresponsive, but I just had to add my experience.

JG

:: sent from my mobile device ::

On Mar 25, 2015, at 20:47, James Ide [email protected] wrote:

FYI transferring data back and forth between JS and Obj-C is relatively slow so you'll probably want to keep all of the image data in Obj-C. I don't have numbers but I expect you will see it takes a measurable amount of time to transfer several MB of data. It's a small compared to the amount of time it takes to upload the photo but it's a good idea to keep the JS <=> Obj-C message queue clear.

You could write a tiny bridge module that receives the asset URL and then performs the upload, reporting progress/error/success information back to JS.

— Reply to this email directly or view it on GitHub.

— Reply to this email directly or view it on GitHub https://github.com/facebook/react-native/issues/201#issuecomment-86773682 .

— Reply to this email directly or view it on GitHub.

sahrens avatar Mar 27 '15 06:03 sahrens

~~I faced the same issue. Here's a naive work around for the time being:~~ ~~https://medium.com/@scottdixon/react-native-posting-uploading-ios-camera-roll-images-to-a-server-6c74766154cd~~

~~@ide is correct, it's really slow transferring data back and forth. Thumbnails seem to hold up OK.~~

Here's a better way to read and then upload camera roll/image component data: https://medium.com/@scottdixon/react-native-creating-a-custom-module-to-upload-camera-roll-images-7a3c26bac309

scottdixon-zz avatar Apr 08 '15 18:04 scottdixon-zz

awesome!

jnak avatar Apr 08 '15 19:04 jnak

@scottdixon - awesome :smile:

For the lazy, here's a link @scottdixon's github repo

brentvatne avatar Apr 15 '15 21:04 brentvatne

Check out my react-native-file-transfer. It makes the request directly from Obj-C which is faster.

kamilkp avatar Apr 23 '15 09:04 kamilkp

@kamilkp Thank you for the sharing, the lib is really cool.

lifuzu avatar Jul 06 '15 07:07 lifuzu

@kamilkp your plugin doesn't seem to work any more in 0.8.0-rc, any ideas why? - Nvm it does, for some reason I had to re add it to the project in Xcode.

josebalius avatar Jul 20 '15 20:07 josebalius

This feature is now built into React Native's XHR implementation anyway. You can attach multiple file attachments to a request body by specifying local file or asset library urls.

nicklockwood avatar Jul 20 '15 20:07 nicklockwood

In case anyone is interested: XHR upload example: https://github.com/facebook/react-native/blob/master/Examples/UIExplorer/XHRExample.js#L184 Thanks for shipping it!

ptmt avatar Jul 22 '15 02:07 ptmt

Though it's closed, I wonder how to compress an image before uploading in react-native because the image in camera roll is too big.

narychen avatar Jan 18 '16 04:01 narychen

@narychen the best option is currently to use the ImageEditor module to resize the image, then upload the resized one.

nicklockwood avatar Jan 18 '16 07:01 nicklockwood

It seems ImageEditor not available on android. And the compression quality is not specified. Maybe write a native module is a better choice?

narychen avatar Jan 18 '16 10:01 narychen