我正在尝试通过Web API客户端将文件上传到数据库服务器
VB类如下
Public Class Uploads
Public Property FileName As String
Public Property uploadDateTime As DateTime
Public Property File As Byte()
End Class
然后ASP.NET用户使用ASP.NET Web表单上传文件。收到此文件后,我们需要通过httpClient
将其上传到服务器 If FileUpload1.HasFile Then
Dim newFile As New Uploads()
newFile.FileName=FileUpload1.FileName
newFile.uploadDateTime=DateTime.Now
newFile.File = New Byte(FileUpload1.PostedFile.ContentLength) {}
FileUpload1.FileContent.Read(newFile.File, 0, FileUpload1.PostedFile.ContentLength)
'Following class makes a call to web api
Dim client As New apiClient()
client.Upload(newFile)
End if
Web API客户端 (这是我被击中的地方。我们如何通过http客户端将类对象传递给web api)。
Public Function Upload(ByVal newFile As Uploads)As Boolean
Dim client As New HttpClient()
client.BaseAddress = New Uri("http://localhost:8080/api/Upload")
Dim content = New MultipartFormDataContent()
'What I should do here to make a call to web api (cross domain)
return true
End Function
然后请求来到web api控制器,最终将通过Entity Framework将文件保存到数据库
<HttpPost()> _
Public Sub PostValue(<FromBody()> ByVal newFile As Uploads)
Using db As New DataContext()
db.Uploadss.Add(newFile)
db.SaveChanges()
End Using
End Sub
任何代码段都非常有用