-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upload file as chuncks #8
Comments
I'm not sure I understand what you mean. Are you trying to upload a large file as a single form element without loading the whole thing into memory, or is the server expecting a form with a single file split into several elements? |
I am trying to split the file in XOJO, something like: |
I'm not sure why you'd want to, but this can be done by adding a final Sub SetFormData(sock As HTTPSocket, FormData As Dictionary, Boundary As String)
If Boundary.Trim = "" Then
Boundary = "--" + Right(EncodeHex(MD5(Str(Microseconds))), 24) + "-bOuNdArY"
End If
Static CRLF As String = EndOfLine.Windows
Dim data As New MemoryBlock(0)
Dim out As New BinaryStream(data)
For Each key As String In FormData.Keys
out.Write("--" + Boundary + CRLF)
If VarType(FormData.Value(Key)) = Variant.TypeString Then
out.Write("Content-Disposition: form-data; name=""" + key + """" + CRLF + CRLF)
out.Write(FormData.Value(key) + CRLF)
ElseIf FormData.Value(Key) IsA FolderItem Then
Dim file As FolderItem = FormData.Value(key)
out.Write("Content-Disposition: form-data; name=""" + key + """; filename=""" + File.Name + """" + CRLF)
out.Write("Content-Type: application/octet-stream" + CRLF + CRLF) ' replace with actual MIME Type
Dim bs As BinaryStream = BinaryStream.Open(File)
out.Write(bs.Read(bs.Length) + CRLF)
bs.Close
' BEGIN ADDED PART
ElseIf FormData.Value(Key) IsA Dictionary Then
Dim d As Dictionary = FormData.Value(Key)
Dim bs As BinaryStream = d.Value("stream")
Dim fn As String = d.Value("filename")
Dim offset As UInt64 = d.Value("offset")
Dim length As UInt64 = d.Value("length")
out.Write("Content-Disposition: form-data; name=""" + key + """; filename=""" + fn + """" + CRLF)
out.Write("Content-Type: application/octet-stream" + CRLF + CRLF) ' replace with actual MIME Type
bs.Position = offset
out.Write(bs.Read(length) + CRLF)
' END ADDED PART
End If
Next
out.Write("--" + Boundary + "--" + CRLF)
out.Close
#If RBVersion > 2012 Then
sock.SetRequestContent(data, "multipart/form-data; boundary=" + Boundary)
#else
sock.SetPostContent(data, "multipart/form-data; boundary=" + Boundary)
#endif
End Sub And then: Dim form As New Dictionary
Dim file As FolderItem = GetOpenFolderItem("")
Dim bs As BinaryStream = BinaryStream.Open(file)
Dim part1 As New Dictionary
part1.Value("filename") = file.Name + "1"
part1.Value("stream") = bs
part1.Value("offset") = 0
part1.Value("length") = 512
form.Value("part1") = part1
Dim part2 As New Dictionary
part2.Value("filename") = file.Name + "2"
part2.Value("stream") = bs
part2.Value("offset") = 512
part2.Value("length") = 512
form.Value("part2") = part2
Dim h As New HTTPSocket
SetFormData(h, form, "")
bs.Close
Dim s As String = h.Post("http://www.example.com/", 10) |
Thanks for your help What I want simply is to send multi requests same time (split big file to chunks and send each 10 files at the same time). |
I not sure that can be done with the HTTPSocket class. Maybe if you explained why you need to split up the file in the first place I could give a better suggestion. |
The task is: Hope it is clear. |
The HTTPSocket doesn't support keep-alive; it closes the socket after the first request. You may want to look into using curl or libcurl. |
Thanks. |
Hi
I am working on a XOJO project that I have to split large file into small chunks.
I read the code here:
http://www.boredomsoft.org/file-uploads-form-encodings-and-xojo.bs
It is working great on uploading a single file, but when trying to split the file into chunks and upload them in loop (so each chunk as a file) it is not working but sometimes it uploads one chunk and the others usually I got upload file error = UPLOAD_ERR_PARTIAL
Is there something I have to add to the content or header?
Thanks for your help
The text was updated successfully, but these errors were encountered: