使用.Net HttpWebRequest发布到FileUploadControl

时间:2011-03-23 20:50:30

标签: .net file-upload httpwebrequest

我正在尝试将文件发布到带有文件上传控件的简单页面。我已经使用了这里的代码:HttpWebRequest FileUpload causes OutOfMemoryException并进行了一些调整来发布文件。

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

要调试该过程,我有一个简单的上传表单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)
            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

通话看起来像这样:

uploadFile(Nothing,“http://localhost:53239/TestSite/uploadform.aspx”,“myfile.csv”,“fuFile”,“application / vnd.ms-excel”,New NameValueCollection)

它返回上传控件没有看到文件,但输入流没有。我一定错过了什么。

我尝试过在页面上传并通过FireBug检查标题,发布的标题和数据看起来都一样。

感谢您的帮助。

0 个答案:

没有答案