Asp.net 将文件发布到不同服务器上的ashx页面
我有一个asp.net网站,用户通过文件上载控件选择一些文件。然后需要将文件发布到另一台服务器 我的域名是[http://www.mydomain.com] 我必须上传文件的地址如下:[https://www.externaldomain.com/upload.ashx?asd2t423eqwdq] 我尝试了以下方法:Asp.net 将文件发布到不同服务器上的ashx页面,asp.net,file-upload,ashx,Asp.net,File Upload,Ashx,我有一个asp.net网站,用户通过文件上载控件选择一些文件。然后需要将文件发布到另一台服务器 我的域名是[http://www.mydomain.com] 我必须上传文件的地址如下:[https://www.externaldomain.com/upload.ashx?asd2t423eqwdq] 我尝试了以下方法: Dim uploadedFiles As HttpFileCollection = Request.Files Dim userPostedFile As HttpPostedF
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后参数,用户名和密码
这就是我的代码现在的样子:
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%正确。我认为问题在于我没有正确创建请求。如果我只发布凭据,我会得到一个错误,说我没有文件。。。
有什么想法吗?最终我使用了这里提供的代码 我把它编译成一个dll,并为我的解决方案添加了一个引用。
它可以工作:您需要使用System.Net.WebRequest类并向其他网站发出post请求。您可以在这里找到示例代码。一个程序员在这里有同样的任务要做。这可能会帮助你。我使用了msdn的链接,但我仍然遇到一些麻烦。我编辑了我最初的帖子并添加了新代码。我不知道如何使用post参数发送用户名、密码和内容。