document-picker icon indicating copy to clipboard operation
document-picker copied to clipboard

Getting network error trying to upload copy of file.

Open Fargard opened this issue 8 years ago • 3 comments

Hi there! i'm not sure that this is a bug or i'm doing something wrong.

I got a Network Error (Could not retrieve file for uri content://com.android.providers.downloads.documents/document/31) when i trying to upload copy of file.

First of all here is my code:

addAttachmentWrapper = formData => {
	const { uploadAttachments, detail } = this.props;

	formData.append('incident_uuid', detail.get('uuid'));
	uploadAttachments(formData);
};

handleUpload = () => {
	DocumentPicker.pickMultiple({
		type: [
			DocumentPicker.types.images,
			DocumentPicker.types.plainText,
			DocumentPicker.types.pdf
		]
	}).then(results => results.forEach(result => {
		const formData = new FormData();

		formData.append('file', result);
		this.addAttachmentWrapper(formData);
	}));
};

it's generate FormData with content:

{ _parts: 
    [ [ 'file',
        { size: 1052994,
          name: 'R39ReceiptExt-4.pdf',
          type: 'application/pdf',
          uri: 'content://com.android.providers.downloads.documents/document/31' } ],
      [ 'incident_uuid', '135d48cf-8755-41a6-adf5-b8d7486e7267' ] ] }

i'm using axios to make request:

export const requestToApi = axios.create({
	baseURL: Config.API_URL,
	headers: {
		common: {
			Authorization: '',
			'X-APP-PLATFORM': Platform.OS,
			'Content-Type': 'application/json'
		}
	}
});

uploadAttachment(data) {
	return requestToApi.post('/incident/attachment/upload', data);
}

First I download PDF file to android simulator and send it to the server and it's work. Then i download this PDF again so i'm having two same files in downloads folder and when i'm trying to upload first original file i'm get this network error.

{ [Error: Network Error]
   config: 
    { adapter: [Function: xhrAdapter],
      transformRequest: { '0': [Function: transformRequest] },
      transformResponse: { '0': [Function: transformResponse] },
      timeout: 0,
      xsrfCookieName: 'XSRF-TOKEN',
      xsrfHeaderName: 'X-XSRF-TOKEN',
      maxContentLength: -1,
      validateStatus: [Function: validateStatus],
      headers: 
       { Accept: 'application/json, text/plain, */*',
         Authorization: 'some bearer ...',
         'X-APP-PLATFORM': 'android' },
      method: 'post',
      baseURL: 'some url ...',
      url: 'https://some url .../incident/attachment/upload',
      data: 
       { _parts: 
          [ [ 'file',
              { size: 1052994,
                name: 'R39ReceiptExt-4.pdf',
                type: 'application/pdf',
                uri: 'content://com.android.providers.downloads.documents/document/31' } ],
            [ 'incident_uuid', '135d48cf-8755-41a6-adf5-b8d7486e7267' ] ] } },
   request: 
    { UNSENT: 0,
      OPENED: 1,
      HEADERS_RECEIVED: 2,
      LOADING: 3,
      DONE: 4,
      readyState: 4,
      status: 0,
      timeout: 0,
      withCredentials: true,
      upload: {},
      _aborted: false,
      _hasError: true,
      _method: 'POST',
      _response: 'Could not retrieve file for uri content://com.android.providers.downloads.documents/document/31',
      _url: 'https://some url .../incident/attachment/upload',
      _timedOut: false,
      _trackingName: 'unknown',
      _incrementalEvents: false,
      responseHeaders: undefined,
      _requestId: null,
      _cachedResponse: undefined,
      _headers: 
       { accept: 'application/json, text/plain, */*',
         authorization: 'some bearer ...',
         'x-app-platform': 'android' },
      _responseType: '',
      _sent: true,
      _lowerCaseResponseHeaders: {},
      _subscriptions: [] },
   response: undefined }

I get this only when i'm trying to upload file which have a copy and only on android. Please could some one point me what i'm doing wrong or this is a bug.

## Update.

Trying use fetch() instead axios:

addAttachmentWrapper = formData => {
	const { detail } = this.props;

	formData.append('incident_uuid', detail.get('uuid'));

	AsyncStorage.getItem('token').then(res => {
		fetch(`${Config.API_URL}/incident/attachment/upload`, {
			method: "POST",
			body: formData,
			headers: {
				'Authorization': `Bearer ${res}`,
				'X-APP-PLATFORM': Platform.OS,
				'Content-Type': 'multipart/form-data' //trying with and without this content-type
			},
			credentials: "same-origin"
		})
		.then(response => console.log('response', response))
		.catch(error => console.log('error', error));
	});
};

handleUpload = () => {
	DocumentPicker.pickMultiple({
		type: [
			DocumentPicker.types.images,
			DocumentPicker.types.plainText,
			DocumentPicker.types.pdf
		]
	}).then(results => results.forEach(result => {
		const formData = new FormData();

		formData.append('file', result);
		this.addAttachmentWrapper(formData);
	}));
};

Same error. 'error', [TypeError: Network request failed]

And again it's happen only on android and only with files which have duplicates.

## Second update.

Trying just to read file which have duplicates with react-native-fs:

handleUpload = () => {
	DocumentPicker.pickMultiple({
		type: [
			DocumentPicker.types.images,
			DocumentPicker.types.plainText,
			DocumentPicker.types.pdf
		]
	}).then(results => results.forEach(result => {
		RNFS.readFile(result.uri, 'base64')
			.then(result => console.log(result))
			.catch(error => console.log('FS-error', error));
	}))
	.catch(error => console.log('DP-error', error));
};

And got error 'FS-error', { [Error: ENOENT: no such file or directory, open 'content://com.android.providers.downloads.documents/document/36'] framesToPop: 1, code: 'ENOENT' }

Any other files without duplicates or duplicates itself return normal base64 content without any errors.

Fargard avatar Jun 06 '18 09:06 Fargard

I figured out what was wrong. Problem was with emulator :) Today i was trying to get a real path to files with RNGRP and i notice that one of file has wrong name. I assume that emulator have some issues with duplicates or i am set something wrong in emulator preferences. All working fine on real devices.

That was really confusing experience :)

Fargard avatar Jun 07 '18 15:06 Fargard

hello i'm also face this same error and i'm not using emulator

shivani2911 avatar Nov 04 '20 08:11 shivani2911

same here

pairalabs-suvam avatar Feb 03 '26 12:02 pairalabs-suvam