Getting network error trying to upload copy of file.
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.
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 :)
hello i'm also face this same error and i'm not using emulator
same here