1

I have a asp.net web site where the user chooses some files with a fileUpload Control. Then the files need to be posted to another server

My domain is [http://www.mydomain.com]

The address where i have to upload the files is something like: [https://www.externaldomain.com/upload.ashx?asd2t423eqwdq]

I have tried the following:

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)

But i get an error: The SaveAs method is configured to require a rooted path, and the path 'https://www.externaldomain.com/upload.ashx?asd2t423eqwdq/Core CSS 3.pdf' is not rooted

I searched the internet, but all i could find were examples on how to upload to the page's server.

EDIT: I used HttpWebRequest to access the link and it partialy worked. I also need to send 2 POST parameters, username and password.

This is how my code looks like now:

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

The error i get is "forbidden". The username and password are 100% correct. I think the problem is that i do not create the request correctly. If i post only the credentials i get the error saying that i have no file... Any ideas?

Alex Albu
  • 693
  • 1
  • 12
  • 20
  • You need to use System.Net.WebRequest class and make a post request to other web site. You can find sample code here. http://msdn.microsoft.com/en-us/library/debx8sh9.aspx – Yiğit Yener Apr 25 '12 at 13:03
  • A programmer at this [http://forums.asp.net/t/1612178.aspx](http://forums.asp.net/t/1612178.aspx) had the same task to do.May this could help you. – Ravi Jain Apr 25 '12 at 13:05
  • I used the link from msdn but i still run into some trouble. I edited my initial post and added the new code. I do not know how to send the username, password and the content using post parameters. – Alex Albu May 01 '12 at 09:03

1 Answers1

0

Eventually I used the code provided here http://aspnetupload.com/

I compiled it into a dll and added a reference to my solution. It works :)

Alex Albu
  • 693
  • 1
  • 12
  • 20