Closed bigradish closed 6 years ago
The server response for the upload request should be included in the responded
event of the upload task. You can set the response body in your server code to include anything you want - for example, JSON data, as long as the response itself it's not too large. Then in the responded
event you can access the response body:
task.on("responded", onRespondedEvent);
// ...
function onRespondedEvent(e) {
console.log('server response is:' + e.data);
}
@lini Thank you very much for your answers. I'll have a try. Another question: When I upload an image file, may I use "multipart/form-data" Content-Type? So far, I don't know how to receive the "application/octet-stream" file on my Golang server. Thank you again.
@lini , I have known how to receive the "application/octet-stream" file on my Golang server.
The sample app in this repository examples/SimpleBackgroundHttp shows how to upload in both ways:
@lini Thanks for your answer. I used the multipart upload code in the sample, and tested on my android mobile phone, an error occurred, it showed the addParameter method can't be resolved (at line 150 of the background-http.android.js file). Please check this. Thank you.
The name and value of the parameters you pass can only be of type string. If you are trying to use JSON or another type for the parameter value, you should convert it to string first.
@lini Yes, exactly. Thank you so much. :)
@lini I found that if the server response is a string, the e.data in the on 'responded' handler is a string of string, such as ""server response"". Is this a bug? I tested on android mobile phone.
This is not normal behavior. You can test it using the demo server and demo app included in the plugin. It is going to return a normal string in the response - "Upload complete!"
and not a double quoted string ""Upload complete!""
.
I suggest you check your server code and verify what is written in the response object.
@lini Thanks for your quick response. My server is normal. My server sends the response in JSON format. The e.data seems to keep the JSON data intact. So I use JSON.parse(e.data) to get the data, and then the double quoted string issue disappears. The data returned by the "responded" handler is not parsed even it is a JSON string, right?
The data in the responded
event is simply the body of the server response. There is no processing or JSON parsing done before it reaches you.
For example, the uploaded file's path on the server. Thanks.