1

我有一个 asp.net 网站,用户在其中选择一些带有 fileUpload 控件的文件。然后需要将文件发布到另一台服务器

我的域名是 [http://www.mydomain.com]

我必须上传文件的地址类似于:[https://www.externaldomain.com/upload.ashx?asd2t423eqwdq]

我尝试了以下方法:

Dim uploadedFiles As HttpFileCollection = Request.Files
Dim userPostedFile As HttpPostedFile = uploadedFiles(0)
Dim filePath As String

 filePath = "https://www.externaldomain.com/upload.ashx?asd2t423eqwdq" & "/" & userPostedFile.FileName


userPostedFile.SaveAs(filePath)

但我收到一个错误:SaveAs 方法配置为需要根路径,并且路径“https://www.externaldomain.com/upload.ashx?asd2t423eqwdq/Core CSS 3.pdf”没有根

我搜索了互联网,但我只能找到有关如何上传到页面服务器的示例。

编辑: 我使用 HttpWebRequest 访问该链接,它部分有效。我还需要发送 2 个 POST 参数、用户名和密码。

这就是我的代码现在的样子:

Dim link As String = "https://www.externaldomain.com/upload.ashx?e9879cc77c764220ae80"

Dim req As HttpWebRequest = WebRequest.Create(link)
Dim boundary As String = "-----"
req.ContentType = "multipart/form-data; boundary=" + boundary
req.Method = "POST"

Dim username As String = "test"
Dim userpass As String = "123456"


Dim credentials() As Byte = Encoding.UTF8.GetBytes("username=" & username & "&password=" & userpass & "--\r\n" & boundary & "--\r\n")



    Dim separators() As Byte = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n")

    Dim uploadedFiles As HttpFileCollection = Request.Files //this is where i take the file that the user wants to upload
    Dim userPostedFile As HttpPostedFile = uploadedFiles(0)


    //i convert the file to a byte array
    Dim binaryReader As IO.BinaryReader
    Dim fileBytes() As Byte
    binaryReader = New BinaryReader(userPostedFile.InputStream)
    fileBytes = binaryReader.ReadBytes(userPostedFile.ContentLength)

    //'get the request length
    req.ContentLength += credentials.Length
    req.ContentLength += userPostedFile.ContentLength
    req.ContentLength += separators.Length
    req.ContentLength += 1


    Dim dataStream As Stream
    dataStream = req.GetRequestStream

    dataStream.Write(credentials, 0, credentials.Length)
    dataStream.Write(separators, 0, separators.Length)
    dataStream.Write(fileBytes, 0, fileBytes.Length)

    dataStream.Close()

    Dim response As HttpWebResponse = req.GetResponse

我得到的错误是“禁止的”。用户名和密码100%正确。我认为问题在于我没有正确创建请求。如果我只发布凭据,我会收到错误消息,说我没有文件......有什么想法吗?

4

1 回答 1

0

最终我使用了这里提供的代码http://aspnetupload.com/

我将它编译成一个 dll 并添加了对我的解决方案的引用。有用 :)

于 2012-05-10T13:43:49.477 回答