帖子文件ASHX页面不同的服务器上 [英] Post file to ashx page on different server

查看:293
本文介绍了帖子文件ASHX页面不同的服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在用户选择使用FileUpload控件的一些文件的asp.net网站。然后将文件需要被张贴到另一台服务器

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

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

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

在那里我有上传文件的地址是一样的东西:[https://www.externaldomain.com/upload.ashx?asd2t423eqwdq]

The address where i have to upload the files is something like: [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'不是根

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.

编辑:
我用HttpWebRequest的访问链接,并将其partialy工作。我也需要发送POST 2参数,用户名和密码。

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

这是我的code的外观像现在:

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

我得到的错误是禁止。用户名和密码是100%正确。我认为,问题是,我不正确地创建请求。如果我只张贴的凭据我得到的错误,说我没有文件...
任何想法?

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?

推荐答案

最后,我用这里提供的code 的http:/ /aspnetupload.com/

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

我编译成一个DLL,并添加到我的解决方案的参考。
它的工作原理:)

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

这篇关于帖子文件ASHX页面不同的服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆