与ASP.NET异步上载文件

时间:2010-07-23 05:00:22

标签: asp.net vb.net

我正在尝试进行异步上传操作,但收到此错误消息:

  

发生错误,info =异常   在WebClient请求期间发生。

这是上传功能:

 Private Sub UploadFile()

        Dim uploads As HttpFileCollection
        uploads = HttpContext.Current.Request.Files

        Dim uri As Uri = New Uri("C:\UploadedUserFiles\")
        Dim client = New WebClient

        AddHandler client.UploadFileCompleted, AddressOf UploadFile_OnCompleted

        For i As Integer = 0 To (uploads.Count - 1)

            If (uploads(i).ContentLength > 0) Then
                Dim c As String = System.IO.Path.GetFileName(uploads(i).FileName)

                Try
                    client.UploadFileAsync(uri, c)
                Catch Exp As Exception

                End Try
                End If
            Next i
          End Sub


 Public Sub UploadFile_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)

        Dim client As WebClient = CType(e.UserState, WebClient)

        If (e.Cancelled) Then
            labMessage.Text = "upload files was cancelled"
        End If

        If Not (e.Error Is Nothing) Then
            labMessage.Text = "Error occured, info=" + e.Error.Message
        Else
            labMessage.Text = "File uploaded successfully"
        End If

    End Sub

更新1:

Private Sub UploadFile()

    Dim uploads As HttpFileCollection

    Dim fileToUpload = "C:\Demo\dummy.doc"

    Dim uri As Uri = New Uri("C:\UploadedUserFiles\")
    Dim client = New WebClient

    AddHandler client.UploadFileCompleted, AddressOf UploadFile_OnCompleted

    client.UploadFileAsync(uri, fileToUpload)


End Sub

client.UploadFileAsync(uri,fileToUpload)抛出此错误消息

 Error occured, info=System.Net.WebException: The request was aborted: The request was canceled. at System.Net.FileWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)  

7 个答案:

答案 0 :(得分:4)

通过调用GetFileName,您将截断那里的任何路径信息。您应该为UploadFileAsync提供完整的路径名称。

此外,仅使用e.Error.Message替换e.Error,以便获得包含内部异常的完整错误详细信息。这将提供更多信息,并可能引导您找到答案。

答案 1 :(得分:2)

Dim uri As Uri = New Uri("C:\UploadedUserFiles\") 

上面的说法是错的,你怎么把Uri作为文件系统路径,它应该是“http://”或“https://”,如果你想把它上传到你的本地asp.net网站项目然后你必须有一个类似http://localhost:PORT/UploadedUserFiles的网址...当你执行项目时你会知道端口号。

Dim uri As Uri = New Uri("http://localhost:PORT/Upload.aspx") 

答案 2 :(得分:0)

试试这个:

1)创建一个新类MyWebClient。

Class MyWebClient
  Inherits WebClient

  Protected Overrides Function GetWebRequest(address As Uri) As WebRequest
    Dim req = MyBase.GetWebRequest(address)
    Dim httpReq = TryCast(req, HttpWebRequest)
    If httpReq IsNot Nothing Then
      httpReq.KeepAlive = False
    End If
    Return req
  End Function
End Class

2)使用此类而不是默认的WebRequest。

Private Sub UploadFile()

  Dim uploads As HttpFileCollection
  uploads = HttpContext.Current.Request.Files

  Dim uri As Uri = New Uri("C:\UploadedUserFiles\")
  Dim client = New MyWebClient

  AddHandler ...

我希望这会有所帮助。

答案 3 :(得分:0)

HttpContext.Current.Request.Files的引用我假设您正在运行Web项目中的代码。您不必使用WebClient将文件保存到磁盘。你所要做的就是:

 Private Sub UploadFile()

    Dim uploads As HttpFileCollection
    uploads = HttpContext.Current.Request.Files

    Dim path As String = "C:\UploadedUserFiles\"

    For i As Integer = 0 To (uploads.Count - 1)

        If (uploads(i).ContentLength > 0) Then
            Dim p As String = System.IO.Path.Combine(path, System.IO.Path.GetFileName(uploads(i).FileName))

            uploads(i).SaveAs(p)
        End If
    Next i
End Sub

我对VB不太好..因此可能存在语法问题:)请耐心等待..

答案 4 :(得分:0)

您无法通过此机制异步上传ASP.NET Web请求中的文件。该文件实际上位于http请求的正文中,因此它已经在服务器上。 Request.Files为已经在服务器上的文件提供流。

您可以从客户端使用类似Silverlight或Flash的内容。

答案 5 :(得分:0)

您可能需要调查AJAX Control Toolkit并使用AsyncFileUpload control

上传完成后,控件有一个服务器端事件。 (UploadedComplete) 它还为上传的文件提供了SaveAs(string filename)方法。

简单用法:

标记

<asp:AsyncFileUpload ID="upload" runat="server" OnUploadedComplete="displayFile"/>

背后的代码

protected void displayFile(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
upload.SaveAs("C:\\text.txt");//server needs permission
}

答案 6 :(得分:0)

尝试将标题信息添加到WebClient对象,如下所示:

       var buyIds = _.map(selectedProducts, 'id').join(',');
       http://shopping.com/app/site/backend/additemtocart.nl?c=TSTDRV945986&n=1&buyid=buyIds