但是没有收到文件
Public Sub UploadFile(filePath As String, url As String)
Using httpClient As New HttpClient
Using fileStream As New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim fileContent As StreamContent = New StreamContent(fileStream)
Dim formContent As MultipartFormDataContent = New MultipartFormDataContent()
formContent.Add(fileContent, "file", Path.GetFileName(filePath))
Dim response As HttpResponseMessage = httpClient.PostAsync(url, formContent).Result
End Using
End Using
End Sub
已使用 url 和文件路径调用此方法
在 cs 文件中处理文件上传,就像
if (Request.HttpMethod == "POST")
{
if (Request.Files.Count > 0)
{ HttpFile uploadedFile = Request.Files[0];
string fileName = Path.GetFileName(uploadedFile.FileName);
string filePath = Server.MapPath("~/uploads/" + fileName);
uploadedFile.SaveAs(filePath);
} }
Public Sub UploadFile(filePath As String, url As String)
Using httpClient As New HttpClient
Using fileStream As New FileStream(filePath, FileMode.Open, FileAccess.Read)
Using fileContent As New StreamContent(fileStream)
Using formContent As New MultipartFormDataContent
' Set the "Content-Disposition" header
Dim fileName = Path.GetFileName(filePath)
formContent.Add(fileContent, "file", fileName)
fileContent.Headers.ContentDisposition = New ContentDispositionHeaderValue("attachment") With {
.FileName = fileName
}
' Send the file to the server
Dim response As HttpResponseMessage = httpClient.PostAsync(url, formContent).Result
' Handle the server response if needed
End Using
End Using
End Using
End Using
End Sub