'Send file from one endpoint to another in VB.NET
I need to send a file that I receive in 'testFile1' to 'testFile2'. I don't know if the stream I am creating is the right way to do it or if I have to retrieve the file on 'testFile2' in another way. Any help?
<HttpPost>
<Route("testFile1")>
Function postTestFile1() As IHttpActionResult
Try
Dim files = HttpContext.Current.Request.Files
If files.Count = 0 Then Throw New Exception("No file")
Dim file = files(0)
Dim req As HttpWebRequest = WebRequest.Create("http://localhost:9000/api/test/testFile2")
req.Method = "POST"
req.ContentType = "multipart/form-data"
req.ContentLength = file.ContentLength
Dim stream = req.GetRequestStream
Dim fileData As Byte()
Using BinaryReader As New BinaryReader(file.InputStream, Encoding.UTF8)
fileData = BinaryReader.ReadBytes(file.ContentLength)
End Using
stream.Write(fileData, 0, fileData.Length)
Dim response As WebResponse = req.GetResponse
Catch ex As Exception
Return InternalServerError()
End Try
End Function
<HttpPost>
<Route("testFile2")>
Function postTestFile2() As IHttpActionResult
Try
Dim files = HttpContext.Current.Request.Files
Dim requestStream = HttpContext.Current.Request.InputStream
If files.Count = 0 Then Throw New Exception("No file")
Dim file = files(0)
'Do the necessary with the file
Return Ok()
Catch ex As Exception
Return InternalServerError()
End Try
End Function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
