Skip to content
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

[XmlHttpRequest] xhr.upload.onprogress not working with xhr.send({uri: 'file:///...'}) #15724

Closed
1stman opened this issue Aug 31, 2017 · 2 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@1stman
Copy link

1stman commented Aug 31, 2017

I think, it's a bug

I'm trying to upload video with youtube v3 API, looking on this example.

So, I wrote something like this:

var xhr = new XMLHttpRequest();
xhr.open('PUT', this.url, true);
xhr.setRequestHeader('Content-Type', this.contentType); <=== video/mp4
xhr.setRequestHeader('Content-Range', 'bytes ' + this.offset + '-' + (end - 1) + '/' + this.filesize);
xhr.setRequestHeader('X-Upload-Content-Type', this.filetype);  <=== video/mp4

if (xhr.upload) {
  xhr.upload.addEventListener('progress', this.onProgress);
}
xhr.onload = this.onContentUploadSuccess_.bind(this);
xhr.onerror = this.onContentUploadError_.bind(this);
xhr.send({uri: this.file.path});  <=== file:///.....

and it works, i have xhr.onload and xhr.onerror and video file is uploaded to youtube and played there.

But i haven't xhr.upload.onprogress.

I try xhr.onprogress, xhr.upload.onprogress, xhr.upload.addEventListener - all of this not works.

BUT!
When i use FormData

let data = new FormData();
data.append('File', {
      uri:  source.path,
      type: source.type,
      name: source.name
});
var xhr = new XMLHttpRequest();
  xhr.open('PUT', this.url, true);
  //xhr.setRequestHeader('Content-Type', this.contentType);  
  //=== comment this, because it throw error 'multipart != video/mp4'
  //xhr.setRequestHeader('Content-Range', 'bytes ' + this.offset + '-' + (end - 1) + '/' + this.filesize);
  //=== comment this, because sending data size != filesize
  xhr.setRequestHeader('X-Upload-Content-Type', this.filetype);

  if (xhr.upload) {
    xhr.upload.addEventListener('progress', this.onProgress);
  }
  xhr.onload = this.onContentUploadSuccess_.bind(this);
  xhr.onerror = this.onContentUploadError_.bind(this);
  xhr.send(data); <==== FormData

xhr.upload.onprogress is working. But then youtube api return processing error, because it support only 'video/*' and 'application/octet-stream' and does not support 'multipart/form-data'.

What I do wrong and what I need to do for using xhr.upload.onprogress WITHOUT FormData?

P.S. sorry for my ugly english

  1. react-native v0.47.2:
  2. node v8.2.1:
  3. npm v5.3.0:
  • Target Platform: Android
  • Development Operating System: MacOS
  • Build tools: Android SDK
@hramos
Copy link
Contributor

hramos commented Aug 31, 2017

At this point this doesn't seem like a particular bug in React Native. The issue may be in your own JavaScript code. I know it's frustrating to see your issues closed but I recommend working on this a bit more, and letting us know if you find that this is definitely an issue that occurs due to a bug in React Native itself. Thanks for understanding.

@hramos hramos closed this as completed Aug 31, 2017
@danww
Copy link

danww commented Sep 5, 2017

@1stman: try putting your xhr.upload.onprogress definition before you do your xhr.open. That may help.

allengleyzer pushed a commit to allengleyzer/react-native that referenced this issue Oct 25, 2017
Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
facebook#15724
facebook#11853
allengleyzer added a commit to allengleyzer/react-native that referenced this issue Oct 25, 2017
Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
facebook#15724
facebook#11853
allengleyzer added a commit to allengleyzer/react-native that referenced this issue Oct 25, 2017
Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
facebook#15724
facebook#11853
facebook-github-bot pushed a commit that referenced this issue Nov 14, 2017
Summary:
Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
#15724
#11853

This is a bug fix for functionality that's missing on Android. These events are already working correctly on iOS.

Run the following code on Android, and ensure that events are being sent:
```
const fileUri = 'file:///my_file.dat';
const url = 'http://my_post_url.com/';
const xhr = new XMLHttpRequest();

xhr.upload.onprogress = (event) => {
    console.log('progress: ' + event.loaded + ' / ' + event.total);
}

xhr.onreadystatechange = () => {if (xhr.readyState === 4) console.log('done');}

console.log('start');

xhr.open('POST', url);

xhr.send({ uri: fileUri }); // sending a file (wasn't sending progress)
xhr.send("some big string"); // sending a string (wasn't sending progress)

const formData = new FormData(); formData.set('test', 'data');
xhr.send(formData); // sending form data (was already working)
```

[ANDROID] [BUGFIX] [XMLHttpRequest] - Added progress updates for all XMLHttpRequest upload types

Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
#15724
#11853
Closes #16541

Differential Revision: D6325252

Pulled By: hramos

fbshipit-source-id: 4fe617216293e6f451e2a1af4fa872e8f56d4f93
cdlewis pushed a commit to cdlewis/react-native that referenced this issue Nov 19, 2017
Summary:
Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
facebook#15724
facebook#11853

This is a bug fix for functionality that's missing on Android. These events are already working correctly on iOS.

Run the following code on Android, and ensure that events are being sent:
```
const fileUri = 'file:///my_file.dat';
const url = 'http://my_post_url.com/';
const xhr = new XMLHttpRequest();

xhr.upload.onprogress = (event) => {
    console.log('progress: ' + event.loaded + ' / ' + event.total);
}

xhr.onreadystatechange = () => {if (xhr.readyState === 4) console.log('done');}

console.log('start');

xhr.open('POST', url);

xhr.send({ uri: fileUri }); // sending a file (wasn't sending progress)
xhr.send("some big string"); // sending a string (wasn't sending progress)

const formData = new FormData(); formData.set('test', 'data');
xhr.send(formData); // sending form data (was already working)
```

[ANDROID] [BUGFIX] [XMLHttpRequest] - Added progress updates for all XMLHttpRequest upload types

Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
facebook#15724
facebook#11853
Closes facebook#16541

Differential Revision: D6325252

Pulled By: hramos

fbshipit-source-id: 4fe617216293e6f451e2a1af4fa872e8f56d4f93
bowerman0 pushed a commit to bowerman0/react-native that referenced this issue Dec 5, 2017
Summary:
Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
facebook#15724
facebook#11853

This is a bug fix for functionality that's missing on Android. These events are already working correctly on iOS.

Run the following code on Android, and ensure that events are being sent:
```
const fileUri = 'file:///my_file.dat';
const url = 'http://my_post_url.com/';
const xhr = new XMLHttpRequest();

xhr.upload.onprogress = (event) => {
    console.log('progress: ' + event.loaded + ' / ' + event.total);
}

xhr.onreadystatechange = () => {if (xhr.readyState === 4) console.log('done');}

console.log('start');

xhr.open('POST', url);

xhr.send({ uri: fileUri }); // sending a file (wasn't sending progress)
xhr.send("some big string"); // sending a string (wasn't sending progress)

const formData = new FormData(); formData.set('test', 'data');
xhr.send(formData); // sending form data (was already working)
```

[ANDROID] [BUGFIX] [XMLHttpRequest] - Added progress updates for all XMLHttpRequest upload types

Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
facebook#15724
facebook#11853
Closes facebook#16541

Differential Revision: D6325252

Pulled By: hramos

fbshipit-source-id: 4fe617216293e6f451e2a1af4fa872e8f56d4f93
bowerman0 pushed a commit to bowerman0/react-native that referenced this issue Dec 5, 2017
Summary:
Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
facebook#15724
facebook#11853

This is a bug fix for functionality that's missing on Android. These events are already working correctly on iOS.

Run the following code on Android, and ensure that events are being sent:
```
const fileUri = 'file:///my_file.dat';
const url = 'http://my_post_url.com/';
const xhr = new XMLHttpRequest();

xhr.upload.onprogress = (event) => {
    console.log('progress: ' + event.loaded + ' / ' + event.total);
}

xhr.onreadystatechange = () => {if (xhr.readyState === 4) console.log('done');}

console.log('start');

xhr.open('POST', url);

xhr.send({ uri: fileUri }); // sending a file (wasn't sending progress)
xhr.send("some big string"); // sending a string (wasn't sending progress)

const formData = new FormData(); formData.set('test', 'data');
xhr.send(formData); // sending form data (was already working)
```

[ANDROID] [BUGFIX] [XMLHttpRequest] - Added progress updates for all XMLHttpRequest upload types

Previously, only form-data request bodies emitted upload progress updates. Now,
other request body types will also emit updates.

Addresses issues:
facebook#15724
facebook#11853
Closes facebook#16541

Differential Revision: D6325252

Pulled By: hramos

fbshipit-source-id: 4fe617216293e6f451e2a1af4fa872e8f56d4f93
facebook-github-bot pushed a commit that referenced this issue Feb 3, 2018
…h on closed connection

Summary:
This PR includes the same changes made in #16541, for addressing issues #11853/#15724. It adds upload progress updates for uploads with any request body type, and not just form-data.

Additionally, this PR also includes a commit for fixing an `IllegalStateException` when a user's connection gets closed or times out (issues #10423/#11016). Since this exception was occurring within the progress updates logic, it started being thrown more frequently as a result of adding progress updates to all uploads, which was why the original PR was reverted.

To test the upload progress updates, run the following JS to ensure events are now being dispatched:
```
const fileUri = 'file:///my_file.dat';
const url = 'http://my_post_url.com/';
const xhr = new XMLHttpRequest();

xhr.upload.onprogress = (event) => {
    console.log('progress: ' + event.loaded + ' / ' + event.total);
}

xhr.onreadystatechange = () => {if (xhr.readyState === 4) console.log('done');}

console.log('start');

xhr.open('POST', url);

// sending a file (wasn't sending progress)
xhr.setRequestHeader('Content-Type', 'image/jpeg');
xhr.send({ uri: fileUri });

// sending a string (wasn't sending progress)
xhr.setRequestHeader('Content-Type', 'text/plain');
xhr.send("some big string");

// sending form data (was already working)
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
const formData = new FormData(); formData.append('test', 'data');
xhr.send(formData);
```

To test the crash fix:
In the RN Android project, before this change, set a breakpoint at `mRequestBody.writeTo(mBufferedSink);` of `ProgressRequestBody`, and wait a short while for a POST request with a non-null body to time out before resuming the app. Once resumed, if the connection was closed (the `closed` variable will be set to true in `RealBufferedSink`), an `IllegalStateException` will be thrown, which crashes the app. After the changes, an `IOException` will get thrown instead, which is already being properly handled.

As mentioned above, includes the same changes as #16541, with an additional commit.

[ANDROID] [BUGFIX] [XMLHttpRequest] - Added progress updates for all XMLHttpRequest upload types / fix crash on closed connection

Previously, only form-data request bodies emitted upload progress updates. Now, other request body types will also emit updates. Also, Android will no longer crash on certain requests when user has a poor connection.

Addresses issues: 11853/15724/10423/11016
Closes #17312

Differential Revision: D6712377

Pulled By: mdvacca

fbshipit-source-id: bf5adc774703e7e66f7f16707600116f67201425
Plo4ox pushed a commit to Plo4ox/react-native that referenced this issue Feb 17, 2018
…h on closed connection

Summary:
This PR includes the same changes made in facebook#16541, for addressing issues facebook#11853/facebook#15724. It adds upload progress updates for uploads with any request body type, and not just form-data.

Additionally, this PR also includes a commit for fixing an `IllegalStateException` when a user's connection gets closed or times out (issues facebook#10423/facebook#11016). Since this exception was occurring within the progress updates logic, it started being thrown more frequently as a result of adding progress updates to all uploads, which was why the original PR was reverted.

To test the upload progress updates, run the following JS to ensure events are now being dispatched:
```
const fileUri = 'file:///my_file.dat';
const url = 'http://my_post_url.com/';
const xhr = new XMLHttpRequest();

xhr.upload.onprogress = (event) => {
    console.log('progress: ' + event.loaded + ' / ' + event.total);
}

xhr.onreadystatechange = () => {if (xhr.readyState === 4) console.log('done');}

console.log('start');

xhr.open('POST', url);

// sending a file (wasn't sending progress)
xhr.setRequestHeader('Content-Type', 'image/jpeg');
xhr.send({ uri: fileUri });

// sending a string (wasn't sending progress)
xhr.setRequestHeader('Content-Type', 'text/plain');
xhr.send("some big string");

// sending form data (was already working)
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
const formData = new FormData(); formData.append('test', 'data');
xhr.send(formData);
```

To test the crash fix:
In the RN Android project, before this change, set a breakpoint at `mRequestBody.writeTo(mBufferedSink);` of `ProgressRequestBody`, and wait a short while for a POST request with a non-null body to time out before resuming the app. Once resumed, if the connection was closed (the `closed` variable will be set to true in `RealBufferedSink`), an `IllegalStateException` will be thrown, which crashes the app. After the changes, an `IOException` will get thrown instead, which is already being properly handled.

As mentioned above, includes the same changes as facebook#16541, with an additional commit.

[ANDROID] [BUGFIX] [XMLHttpRequest] - Added progress updates for all XMLHttpRequest upload types / fix crash on closed connection

Previously, only form-data request bodies emitted upload progress updates. Now, other request body types will also emit updates. Also, Android will no longer crash on certain requests when user has a poor connection.

Addresses issues: 11853/15724/10423/11016
Closes facebook#17312

Differential Revision: D6712377

Pulled By: mdvacca

fbshipit-source-id: bf5adc774703e7e66f7f16707600116f67201425
@facebook facebook locked as resolved and limited conversation to collaborators Aug 31, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Aug 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants