vb.net 在VB.NET中上传文件并读取服务器响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25516674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:06:42  来源:igfitidea点击:

Upload a file in VB.NET and read the server response

vb.netfilepostupload

提问by JS96

I need to upload a file on a server and I need to receive the response (the url where the file was upload).

I have two things:
- URL (the site to which I have to make the request)
- Filename (the path of the file which I want to upload)

我需要在服务器上上传一个文件,我需要接收响应(上传文件的 url)。

我有两件事:
- URL(我必须向其发出请求的站点)
- 文件名(我要上传的文件的路径)

And I want to get:
- File uploaded on that server
- The response of the server

我想得到:
- 在该服务器上上传的文件
-服务器的响应

I searched for hours for a solution, but I found only source in C#, that I also tried to convert in VB.NET, but it doesn't work.

我搜索了几个小时的解决方案,但我只找到了 C# 中的源代码,我也尝试在 VB.NET 中进行转换,但它不起作用。

Thanks!

谢谢!

回答by Abdes sabor



There are two ways you can upload a file to a server.

有两种方法可以将文件上传到服务器。



What server are you using?
if you're using a php server, you can create a directory named uploada file with the name upload.phpand set it'd content to this code :

你用的是什么服务器?
如果您使用的是 php 服务器,则可以upload使用该名称创建一个名为文件的目录upload.php,并将其内容设置为以下代码:

<?   
    move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
    echo realpath("upload/" . $_FILES["file"]["name"]); 
?>

on the vb.net code you should use the code below :

在 vb.net 代码上,您应该使用以下代码:

Dim Fileuri As String
Using we As New WebClient
    Dim responseArray As Byte()
    responseArray = we.UploadFile("http://www.yourdomain.com/upload.php", Filepath)
    Fileuri = System.Text.Encoding.ASCII.GetString(responseArray)
End Using


on the other hand, you can use ftp protocole to upload files to your server :

另一方面,您可以使用 ftp protocole 将文件上传到您的服务器:

Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://yourdomain.com/upload/" & Compresseduri), System.Net.FtpWebRequest)
request.Credentials = New System.Net.NetworkCredential("username", "password")
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile

Dim files() As Byte = System.IO.File.ReadAllBytes(filetoupload)

Dim strz As System.IO.Stream = request.GetRequestStream()
strz.Write(files, 0, files.Length)
strz.Close()
strz.Dispose()

Best wishes,

最好的祝愿,