Using .Net HttpWebRequest to post to FileUploadControl
I'm trying to post a file to a simple page with a file upload control. I've used the code from here: HttpWebRequest FileUpload causes OutOfMemoryException with some tweaks to post the file.
Public Function uploadFile(ByVal containa As CookieContainer, ByVal uri As String, ByVal filePath As String, ByVal fileParameterName As String, ByVal contentType As String, ByVal otherParameters As Specialized.NameValueCollection, Optional UserName As String = "", Optional Password As String = "") As String
'https://stackoverflow.com/questions/4388689/vb-net-httpwebrequest-fileupload
Dim boundary As String = "---------------------------" & DateTime.Now.Ticks.ToString("x")
Dim newLine As String = System.Environment.NewLine
Dim boundaryBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(newLine & "--" & boundary & newLine)
Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
'Set credentials
If UserName <> "" And Password <> "" Then
Dim NetworkCred As New Net.NetworkCredential(UserName, Password)
request.Credentials = NetworkCred
End If
request.ContentType = "multipart/form-data; boundary=" & boundary
request.Method = "POST"
request.CookieContainer = containa
request.AllowAutoRedirect = True
request.Timeout = -1
request.KeepAlive = True
request.AllowWriteStreamBuffering = True
Dim ms As New MemoryStream()
Dim formDataTemplate As String = "Content-Disposition: form-data; name=""{0}""{1}{1}{2}"
For Each key As String In otherParameters.Keys
ms.Write(boundaryBytes, 0, boundaryBytes.Length)
Dim formItem As String = String.Format(formDataTemplate, key, newLine, otherParameters(key))
Dim formItemBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(formItem)
ms.Write(formItemBytes, 0, formItemBytes.Length)
Next key
ms.Write(boundaryBytes, 0, boundaryBytes.Length)
Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2}Content-Type: {3}{2}{2}"
Dim header As String = String.Format(headerTemplate, fileParameterName, filePath, newLine, contentType)
Dim headerBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(header)
ms.Write(headerBytes, 0, headerBytes.Length)
Dim length As Long = ms.Length
length += New FileInfo(filePath).Length
request.ContentLength = length
Using requestStream As IO.Stream = request.GetRequestStream()
Dim bheader() As Byte = ms.ToArray()
requestStream.Write(bheader, 0, bheader.Length)
Using fileStream As New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read)
Dim buffer(4096) As Byte
Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length)
Do While (bytesRead > 0)
requestStream.Write(buffer, 0, bytesRead)
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
Loop
End Using
requestStream.Close()
End Using
Dim response As Net.WebResponse = Nothing
Dim responseText = ""
Try
response = request.GetResponse()
Using responseStream As IO.Stream = response.GetResponseStream()
Using responseReader As New IO.StreamReader(responseStream)
responseText = responseReader.ReadToEnd()
End Using
End Using
Catch exception As Net.WebException
responseText = exception.Message
Finally
response.Close()
response = Nothing
request = Nothing
End Try
Return responseText
End Function
To debug the process I have a simple upload form UploadForm.aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="UploadForm.aspx.vb" Inherits="UploadForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Upload Test Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal runat="server" ID="litInfo" EnableViewState="false"></asp:Literal><br />
<asp:FileUpload runat="server" ID="fuFile" />
<asp:LinkButton runat="server" ID="btnUpload" Text="Upload"></asp:LinkButton>
</div>
</form>
</body>
</html>
UploadForm.aspx.vb
Imports System.Reflection
Partial Class UploadForm
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If fuFile.HasFile Then
litInfo.Text &= "File Upload Control sees a file<br />"
Else
litInfo.Text &= "File Upload Control does NOT see a file<br />"
End If
litInfo.Text &= "File Upload Count: " & Request.Files.Count & "<br />"
If IsNothing(Request.InputStream) = False Then
Using responseReader As New IO.StreamReader(Request.InputStream)
litInfo.Text &= "<h3>Input Stream</h3>" & responseReader.ReadToEnd.Replace(vbCrLf, "<br />")
responseReader.Close()
End Using
End If
litInfo.Text &= "<h3>Request Properties</h3>" & ListProperties(Request).Replace(vbCrLf, "<br />")
litInfo.Text &= "<h3>Headers</h3>"
For Each Item As String In Request.Headers.Keys
litInfo.Text &= Item & ": " & Request.Headers(Item) & "<br />"
Next
End Sub
Function ListProperties(ByVal TheObject As Object) As String
Dim property_value As Object
Dim TypeInfo As Type = TheObject.GetType
Dim properties_info As PropertyInfo() = TypeInfo.GetProperties()
Dim ReturnBuilder As New System.Text.StringBuilder
For i As Integer = 0 To properties_info.Length - 1
With properties_info(i)
If .GetIndexParameters().Length = 0 Then
Try
property_value = .GetValue(TheObject, Nothing)
If property_value Is Nothing Then
ReturnBuilder.AppendLine(.Name & ": " & "Nothing")
Else
ReturnBuilder.AppendLine(.Name & ": " & property_value.ToString)
End If
Catch ex As Exception
ReturnBuilder.AppendLine(.Name & ": Error")
End Try
Else
ReturnBuilder.AppendLine(.Name & ": " & .PropertyType.ToString)
开发者_Go百科 End If
End With
Next i
Return vbCrLf & ReturnBuilder.ToString
End Function
End Class
myfile.csv
field1,field2,field3
1,2,3
4,5,6
7,8,9
Call looks like this:
uploadFile(Nothing, "http://localhost:53239/TestSite/uploadform.aspx", "myfile.csv", "fuFile", "application/vnd.ms-excel", New NameValueCollection)
It returns back that the upload control doesn't see a file but the input stream does. I must be missing something.
I've tried the upload in page and inspected the headers via FireBug and the posted headers and data look the same.
Thanks for any assistance.
精彩评论