See upload progress of file upload
Is there a way to see the how many bytes have been uploaded already when uploading a file (as a multipart field)? I've tried displaying the reading progress of the reader, but that doesn't work because resty seems to read the entire file at once.
(I'm new to Go, so I apologize if there is an obvious solution to this that I'm just not seeing.)
@FM-96 I'm sorry for the delayed response. Resty does read the entire request body in the memory before sending request BUT NOT ALL THE CASES.
If Body is supplied to Resty as interface io.Reader then resty does not read entire content instead it passes that interface to Go HTTP client as-is.
So you can build progress info at your end 😄
That is weird then. I am passing an io.Reader to it (I think), but it is definitely reading the whole file into memory first.
This is the relevant part of my code, for reference:
file, err = os.Open(video.Video)
contentType = resty.DetectContentType(file)
file.Seek(0, 0)
stat, err = file.Stat()
progress = newUploadProgressReader(file, stat.Size())
resp, err = client.R().
SetHeader("Referer", uploadLink).
SetFormData(map[string]string{
"csrfmiddlewaretoken": csrfToken,
"upload_code": uploadData.uploadCode,
"upload_type": "video",
}).
SetMultipartField("file", file.Name(), contentType, progress).
Post(baseUploadLink)
@FM-96 You code snippet example is multipart form, it is not the use case what I referred above for io.Reader.
Yes, like I mentioned in the original issue, I'm uploading a file as a multipart field. So there is no way to do what I need with this library then?
@FM-96 I understand and I agree about your original issue. I have overlooked the word multipart.
I'm sorry for the confusion I have caused with my initial response about io.Reader.
Let's get the facts clear this time.
Per HTTP RFC specification, Its not feasible to build file upload progress info since multipart data sent with boundary metadata and complete file content and form fields. Basically not only in Resty library; in general, is it not feasible to build upload progress.
Next, It is certainly possible if file content sent to the dedicated endpoint on the server-side. It means the request body contains one file as binary data. This is typical way of uploading files other than multipart. This is feasible in Resty and most HTTP client library which supports the body as streaming.
I hope this info clarifies it better.