在vb.net中通过post方法发送文件到服务器,服务器端接收并保存到文件夹中

问题描述 投票:0回答:1

但是没有收到文件

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);       
  }  }
asp.net vb.net webforms
1个回答
0
投票
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
© www.soinside.com 2019 - 2024. All rights reserved.