使用VB.net和php将文件上传到网站 [英] Uploading a file to a website using VB.net and php
问题描述
你好!
我正在尝试使用php将文件上传到自制网站.
我可以使用服务器上的html文件上载文件,但是我想使用VB.net 应用程序来完成文件.
这是我已经在网上找到(并编辑)的内容.
当我想运行此代码时,在此行中收到错误:
以HttpWebResponse = DirectCast(request.GetResponse(),HttpWebResponse)的昏暗响应
通常,当我要上传普通数据(例如名称或其他字符串,...)时,最后10行代码可以正常工作.
怎么了?我的标头?还是我想将其写入流中的方式,...?
Hello!
I''m trying to upload a file to a self-made website using php.
I can upload a file using a html file on the server but I wanted to do it with VB.net app.
This is what I already found (and edited) on the net.
When I want to run this code, I receive an error in this line:
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
Normally the last 10 lines of code works ok when I wanted to upload normal data (e.g. a name or other string,...).
What''s wrong? My header? Or the way I want to write it to the stream, ...?
<pre lang="vb">Function SendFile(ByVal filepath As String, ByVal feedbackUrl As String)<br />
Dim boundery As String = IO.Path.GetRandomFileName<br />
Dim header As New System.Text.StringBuilder()<br />
header.AppendLine("--" & boundery)<br />
header.Append("Content-Disposition: form-data; name=""userfile"";")<br />
header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(filepath))<br />
header.AppendLine()<br />
header.AppendLine("Content-Type: application/octet-stream")<br />
header.AppendLine()<br />
<br />
''Convert in binary format (ASCII)<br />
Dim headerbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(header.ToString)<br />
Dim endboundarybytes() As Byte = System.Text.Encoding.ASCII.GetBytes(vbNewLine & "--" & boundery & "--" & vbNewLine)<br />
<br />
''Create and initialize the request<br />
Dim request As HttpWebRequest = DirectCast(WebRequest.Create(feedbackUrl), HttpWebRequest)<br />
request.UserAgent = "My Program Agent"<br />
request.Method = "POST"<br />
<br />
''Write POST data to request<br />
request.ContentType = "multipart/form-data; boundary=" & boundery<br />
request.ContentLength = headerbytes.Length + New FileInfo(filepath).Length + endboundarybytes.Length<br />
Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filepath)<br />
Dim requestStream As System.IO.Stream = request.GetRequestStream()<br />
requestStream.Write(headerbytes, 0, headerbytes.Length)<br />
requestStream.Write(filebytes, 0, filebytes.Length)<br />
requestStream.Write(endboundarybytes, 0, endboundarybytes.Length)<br />
requestStream.Close()<br />
<br />
''Execute request and read response.<br />
Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)<br />
Dim streamgt As System.IO.Stream = response.GetResponseStream()<br />
Dim length As Int64 = 100000<br />
Dim txt(length) As Byte<br />
streamgt.Read(txt, 0, length)<br />
streamgt.Close()<br />
streamgt.Dispose()<br />
Return txt<br />
End Function</pre><br />
推荐答案
我的最初反应是您的边界字符串可能包含非法字符.
尝试将边界设置为文字字符串,例如"1234567890-0987654321",然后重试.如果它确实寻求另一种获取随机边界的方法,则可能会解决该问题.
My initial reaction is that your boundary sting may contain illegal characters.
Try setting your boundary to literal string like "1234567890-0987654321" and try again. That may resolve it, if it does look into another method of getting a random boundary.
这篇关于使用VB.net和php将文件上传到网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!