(图片传输)用vb.net(可以是其他语言)向.net (api..aspx)接口post二进制文件流怎样在.net里取得值并返回结果.(也就是对post传输"multipart/form-data"的流进行处理与返回)
现小弟写一个接口~处理对任何语言传过来的文件流(以post方式"multipart/form-data")主要是上传本地图片到HTTP的一个平台,进行取二进制流图片的读取还原并保存到数据库,,,,且操作后返回信息给传过来的客户端~现对VB.NET传输进行实现~但是我没有用过不知道怎样取值后进行二进制图片文件流进行操作并还原而且还要返回结果给VB.NET客户端.....小弟跪求啊......24小时在线等在线散分....谢谢前辈了... --------------------编程问答-------------------- 补充一下~VB.NET 代码~
ASP.NET post 页面 api.aspx
Private Sub btnPostCaptcha_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPostCaptcha.Click
If txtAPIKey.Text = "" Or txtCaptchaImage.Text = "" Then
MsgBox("API Key and Captcha Image file must be specified")
Exit Sub
End If
txtCaptchaID.Text = postCaptcha(txtAPIKey.Text, txtCaptchaImage.Text)
'strip out our sucess indicator to just get the captcha ID
If InStr(txtCaptchaID.Text, "SUCCESS: captcha_id=", CompareMethod.Text) > 0 Then
txtCaptchaID.Text = Replace(txtCaptchaID.Text, "SUCCESS: captcha_id=", "")
txtCaptchaID.Text = Replace(txtCaptchaID.Text, vbCr, "")
txtCaptchaID.Text = Replace(txtCaptchaID.Text, vbLf, "")
cmdGetResult.Enabled = True
Else
MsgBox("Error uploading captcha. API Result: " & txtCaptchaID.Text)
cmdGetResult.Enabled = False
End If
End Sub
Public Function postCaptcha(ByVal strApiKey As String, ByVal strCaptchaFile As String) As String
Dim objEncoding As New System.Text.UTF8Encoding
Dim objStreamWriter As System.IO.StreamWriter
Dim objStream As System.IO.Stream
Dim objHTTPRequest As HttpWebRequest
Dim sbPostData As New System.Text.StringBuilder
Dim intUploadBit As Integer
Dim intUploadSoFar As Integer
Dim inttoUpload As Integer
Dim i As Integer
Dim objStreamReader As System.IO.Stream
Dim strResult As String
'set request properties
objHTTPRequest = System.Net.WebRequest.Create("http://localhost:1116/captch易做图/api.aspx")
objHTTPRequest.AllowAutoRedirect = True
objHTTPRequest.Accept = "*/*"
objHTTPRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)"
objHTTPRequest.KeepAlive = False
objHTTPRequest.Timeout = 30000
objHTTPRequest.Method = "POST"
objHTTPRequest.ContentType = "multipart/form-data; boundary=7cf2a327f01ae"
sbPostData.Append("--" + "7cf2a327f01ae" + vbCrLf)
sbPostData.Append("Content-Disposition: form-data; name=""api_key""" + vbCrLf)
sbPostData.Append(vbCrLf)
sbPostData.Append(strApiKey + vbCrLf)
sbPostData.Append("--" + "7cf2a327f01ae" + vbCrLf)
sbPostData.Append("Content-Disposition: form-data; name=""expire""" + vbCrLf)
sbPostData.Append(vbCrLf)
sbPostData.Append("1000" + vbCrLf) 'defaulted to 1000 but you can change this
sbPostData.Append("--" + "7cf2a327f01ae" + vbCrLf)
sbPostData.Append("Content-Disposition: form-data; name=""method""" + vbCrLf)
sbPostData.Append(vbCrLf)
sbPostData.Append("upload_captcha" + vbCrLf)
sbPostData.Append("--" + "7cf2a327f01ae" + vbCrLf)
sbPostData.Append("Content-Disposition: form-data; name=""rights""" + vbCrLf)
sbPostData.Append(vbCrLf)
sbPostData.Append("false" + vbCrLf) 'defaulted to false but you can change this
'this is the header for our captcha file upload
sbPostData.Append("--" + "7cf2a327f01ae" + vbCrLf)
sbPostData.Append("Content-Disposition: form-data; name=""file""; filename=""" & strCaptchaFile & "" + vbCrLf)
sbPostData.Append("Content-Type: image/pjpeg" + vbCrLf)
sbPostData.Append(vbCrLf)
'read our captch into a byte array
Dim objBinReader As New BinaryReader(File.OpenRead(strCaptchaFile))
Dim bytPhotoContents As Byte() = objBinReader.ReadBytes(objBinReader.BaseStream.Length)
objBinReader.Close()
'convert our other post data into a byte array
Dim bytPostContents As Byte() = objEncoding.GetBytes(sbPostData.ToString)
'create a footer for insertation after the file bytes are uploaded
Dim bytPostFooter As Byte() = objEncoding.GetBytes(vbCrLf + "--" + "7cf2a327f01ae" + vbCrLf)
'create a new data buffer to hold all of our byte arrays
Dim bytDataBuffer As Byte() = New Byte(bytPostContents.Length + bytPhotoContents.Length + bytPostFooter.Length) {}
'copy the contents of our three byte arrays into our single byte array
System.Buffer.BlockCopy(bytPostContents, 0, bytDataBuffer, 0, bytPostContents.Length)
System.Buffer.BlockCopy(bytPhotoContents, 0, bytDataBuffer, bytPostContents.Length, bytPhotoContents.Length)
System.Buffer.BlockCopy(bytPostFooter, 0, bytDataBuffer, bytPostContents.Length + bytPhotoContents.Length, bytPostFooter.Length)
'set the content length based on our new byte array length
objHTTPRequest.ContentLength = bytDataBuffer.Length
'get our stream and post our data
objStream = objHTTPRequest.GetRequestStream()
'chunk up our data and upload it to our stream
'will generally only need to send in one chunk unless file is large
intUploadBit = Math.Max(bytDataBuffer.Length / 100, 50 * 1024)
intUploadSoFar = 0
While i < bytDataBuffer.Length
inttoUpload = Math.Min(intUploadBit, bytDataBuffer.Length - i)
intUploadSoFar += inttoUpload
objStream.Write(bytDataBuffer, i, inttoUpload)
i = i + intUploadBit
End While
'close our stream
objStream.Close()
'get the response from the server
Dim objHTTPResponse As HttpWebResponse = CType(objHTTPRequest.GetResponse(), HttpWebResponse)
objStreamReader = objHTTPResponse.GetResponseStream()
'final result from server is returned to strResult
Dim objStreamResult As New System.IO.StreamReader(objStreamReader)
strResult = objStreamResult.ReadToEnd
'close our objects
objStreamReader.Close()
objStreamResult.Close()
'Return strResult
End Function --------------------编程问答-------------------- 你为什么不能直接先存到数据库中,然后再从数据库中读出来呢... --------------------编程问答-------------------- 因为我这个传过来的图片是任何的客户端的~所以图片是在客户端的本地~必须要上传到我这个平台也就是平台服务器端.......
补充:.NET技术 , ASP.NET