nativescript-http-formdata icon indicating copy to clipboard operation
nativescript-http-formdata copied to clipboard

Problem to send file

Open njtnestor opened this issue 5 years ago • 5 comments

Hello I am using a Mediafilepicker to grab a file with nativescript, but as example to test I created a file of next way:

let stream = new java.io.ByteArrayOutputStream();
stream.write(5)
let file = stream.toByteArray()

After i do this:

let fd = new TNSHttpFormData();
let params = []
let param = {
     data: file,
     parameterName: 'file'
}
params.push(param)
try{
       const response = await fd.post('https://example.com/file', params, {
         headers: {
            //some headers
         }
       });
            console.log(response)
} catch(e){
     console.log('error:', e)
}

As result I got my app blocked and crashed without errors

P.D: I'm using javascript instead typescript

njtnestor avatar May 18 '20 10:05 njtnestor

@njtnestor can you please pull this project and run the demo and check ? i havn't tested on JS...

dotnetdreamer avatar May 19 '20 22:05 dotnetdreamer

I can confirm that this happens to me too. I see that the POST request is made to the API, but fd.post do not return and crash the app in Android 7.1.1. In Android 10 it simply hangs forever. The strange thing is that this was working before. Any solutions for this?

andresilva-cc avatar Jul 01 '20 17:07 andresilva-cc

the same here

vallemar avatar Apr 27 '22 10:04 vallemar

Had the same problem with a request getting stuck and therefore crushing the Android app. What I found the reason was in https://github.com/dotnetdreamer/nativescript-http-formdata/blob/f130ccdb137a6cb3bfbeffb27fc38c0cebc5a1bd/src/TNSHttpFormData.android.ts#L49 When you call any methods on response object (i.e. body(), headers(), code() or message()) they are never resolved. Therefore, the solution that worked for me was to copy the whole class TNSHttpFormData (https://github.com/dotnetdreamer/nativescript-http-formdata/blob/master/src/TNSHttpFormData.android.ts) and 2 related imported files:

  • https://github.com/dotnetdreamer/nativescript-http-formdata/blob/master/src/index.d.ts
  • https://github.com/dotnetdreamer/nativescript-http-formdata/blob/master/src/TNSHttpFormData.common.ts into local project and remove all instances in TNSHttpFormData where any methods were called on response object. Example let callback = new okhttp3.Callback({ onResponse: (call, response) => { resolve({ headers: "", statusCode: 202, statusMessage: "202", body: "" }); }, // incase of timeout etc, this will be called onFailure: (call, response) => { reject(response); } });

SidnevIgor avatar May 03 '22 08:05 SidnevIgor

i can confirm that this is an issue. Any chance that this will be fixed, @dotnetdreamer? Would highly appreciate it :)

timoschlueter avatar Jan 08 '23 10:01 timoschlueter