2012-04-25 89 views
0

我有一個asp.net網站,用戶通過fileUpload控件選擇一些文件。然後把這些文件需要被張貼到另一臺服務器將文件發佈到不同服務器上的ashx頁面

我的域爲[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方法被配置爲需要有根路徑,拍拍h'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%正確的。我認爲問題是我沒有正確創建請求。如果我只發佈證書,我得到的錯誤說我沒有檔案... 任何想法?

+0

您需要使用System.Net.WebRequest類並向其他網站發出發佈請求。你可以在這裏找到示例代碼。 http://msdn.microsoft.com/en-us/library/debx8sh9.aspx – 2012-04-25 13:03:06

+0

這是一個程序員[http://forums.asp.net/t/1612178.aspx](http://forums.asp。 net/t/1612178.aspx)有同樣的任務。可以幫助你。 – 2012-04-25 13:05:07

+0

我使用msdn的鏈接,但我仍遇到一些麻煩。我編輯了我的初始帖子並添加了新的代碼。我不知道如何使用發佈參數發送用戶名,密碼和內容。 – 2012-05-01 09:03:05

回答

0

最後我用這裏提供的代碼http://aspnetupload.com/

我編譯成一個dll,並添加到我的解決方案的參考。 它的工作原理:)

相關問題