将文件上传到Web服务器 [英] uploading a file to webserver

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

问题描述



我在vb.net中有一个窗口应用程序.我需要通过窗口应用程序(桌面)将一些文件上传到Web服务器(例如http:/www.domain.com/directory).

我是vb.net的新手.所以请帮我解决我的问题.

谢谢

Hi,

I have a window application in vb.net. I need to upload some file to webserver (e.g. http:/www.domain.com/directory) form my window application (desktop).

I am new in vb.net. so please help me regarding my problem.

Thanks

推荐答案

如果该网站是您的网站,则可以使用文件上传界面.在Windows应用程序中使用WebBrowser控件访问网页.上载文件.

否则您的domain.com具有ftp服务,则可以使用FtpWebRequest类.这是一个例子.

http://www.vcskicks.com/csharp_ftp_upload.php [
If that website is yours then can have a interface with file upload. Access the webpage using WebBrowser control in your windows application. Upload the file.

Or else your domain.com has ftp service then you can use the FtpWebRequest class. Here is an example.

http://www.vcskicks.com/csharp_ftp_upload.php[^]


您需要使用HttpWebRequest对象.我找到了这段代码(google是免费的),但是它需要一些try/catch东西,对其进行一些优化并不是一个坏主意.使用网络上可用的众多免费转换器之一,将其转换为VB不会有任何麻烦.

You need to use the HttpWebRequest object. I found this code (google is free), but it needs some try/catch stuff put in, and it wouldn''t be a bad idea to optimize it a little. You shouldn''t have any trouble converting it to VB with one of the many free converters available on the web.

public static  void UploadFilesToRemoteUrl(string url, string[] files, string logpath, NameValueCollection nvc) 
{      
    long length = 0;     
    string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");       
    HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
    httpWebRequest2.Method      = "POST";
    httpWebRequest2.KeepAlive   = true; 
    httpWebRequest2.Credentials = System.Net.CredentialCache.DefaultCredentials;    

    Stream memStream = new System.IO.MemoryStream();  
    byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");   
    string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
    foreach (string key in nvc.Keys) 
    {    
        string formitem = string.Format(formdataTemplate, key, nvc[key]); 
        byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); 
        memStream.Write(formitembytes, 0, formitembytes.Length); 
    } 
    memStream.Write(boundarybytes, 0, boundarybytes.Length);  
    string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";  
    for (int i = 0; i < files.Length; i++) 
    { 
        //string header = string.Format(headerTemplate, "file" + i, files[i]);  
        string header = string.Format(headerTemplate, "uplTheFile", files[i]);   
        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);  
        memStream.Write(headerbytes, 0, headerbytes.Length);  
        FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read);  
        byte[] buffer = new byte[1024]; 
        int bytesRead = 0;  
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)  
        {   
            memStream.Write(buffer, 0, bytesRead);
        } 
        memStream.Write(boundarybytes, 0, boundarybytes.Length); 
        fileStream.Close(); 
    } 
    httpWebRequest2.ContentLength = memStream.Length;  
    Stream requestStream = httpWebRequest2.GetRequestStream();  
    memStream.Position = 0;     
    byte[] tempBuffer = new byte[memStream.Length]; 
    memStream.Read(tempBuffer, 0, tempBuffer.Length); 
    memStream.Close(); 
    requestStream.Write(tempBuffer, 0, tempBuffer.Length); 
    requestStream.Close();   
    WebResponse webResponse2 = httpWebRequest2.GetResponse();  
    Stream stream2 = webResponse2.GetResponseStream(); 
    StreamReader reader2 = new StreamReader(stream2);   
    MessageBox.Show(reader2.ReadToEnd());  
    webResponse2.Close(); 
    httpWebRequest2 = null; 
    webResponse2 = null; 
}


有两种方法可以做到这一点.

您可以选择在网络服务器上安装FTP服务器,然后选择实现
FTP客户端 [ ^ ].

您还可以将Web服务添加到您的网站,以接受文件上传.

如果您能够安装FTP服务器,则我建议使用FTP选项,因为FTP可以更好地将文件从一个位置发送到另一个位置,而Web服务则更好.

祝你好运!
爱德华
There are a couple of ways to do that.

You can either choose the install a FTP server on the webserver and choose to implement an FTP Client[^] in your software.

You can also add a webservice to your website which accepts file upload.

I recommend using the FTP option if you''re able to install a FTP Server since FTP is way better in sending files from one location to another then a webservice is.

Good luck!
Eduard


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

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