2011-08-31 54 views
0

我在C#中创建了一个窗体窗体应用程序。有一个用户注册部分,其中填写用户详细信息并上传照片。如何将照片上传到服务器而不是客户端系统中的公共位置。我需要将用户的图片上传到服务器中的一个位置,以便应用程序的网站部分可以在用户的​​配置文件中显示图片。如何从.net winforms上传照片到服务器?

+0

这个网站是在您的内部网上还是在外部的互联网上? – Filburt

+0

该网站是一个在线版本。 – deepu

回答

2

我实际上会将包括图片在内的信息存储在数据库中,所以它可以从所有其他应用程序中使用。

,如果你只是想从客户端计算机的原始文件复制到一个集中的位置,作为一个起点:

private void button1_Click(object sender, EventArgs e) 
{ 
    WebClient myWebClient = new WebClient(); 
    string fileName = textBox1.Text; 
    string _path = Application.StartupPath; 
    MessageBox.Show(_path); 
    _path = _path.Replace("Debug", "Images"); 
    MessageBox.Show(_path); 
    myWebClient.UploadFile(_path,fileName); 
} 

private void btnBrowse_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog ofDlg = new OpenFileDialog(); 
    ofDlg.Filter = "JPG|*.jpg|GIF|*.gif|PNG|*.png|BMP|*.bmp"; 
    if (DialogResult.OK == ofDlg.ShowDialog()) 
    { 
     textBox1.Text = ofDlg.FileName; 
     button1.Enabled = true; 
    } 
    else 
    { 
     MessageBox.Show("Go ahead, select a file!"); 
    } 
} 
+0

是的,我想将图像存储到在线服务器的位置,如“http // Sitename/Images/uploadedPhoto”而不是客户端系统 – deepu

1

也许最好的办法是使用FTP服务器,如果你可以安装它。 比你能文件的使用此代码

FileInfo toUpload = new FileInfo("FileName"); 
System.Net.FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://serverip/FileName"); 
request.Method = WebRequestMethods.Ftp.UploadFile; 
request.Credentials = new NetworkCredential("UserName","Password"); 
Stream ftpStream = request.GetRequestStream(); 
FileStream file = File.OpenRead(files); 
int length = 1024; 
byte[] buffer = new byte[length]; 
int bytesRead = 0; 
do 
{ 
    bytesRead = file.Read(buffer, 0, length); 
    ftpStream.Write(buffer, 0, bytesRead); 
} 
while (bytesRead != 0); 
file.Close(); 
ftpStream.Close(); 
+0

如何将文件存储到非FTP服务器 – deepu

+0

http://stackoverflow.com/questions/ 566462 /上载的文件与 - HttpWebRequest的,多形式的数据 – Burimi

0

使用C#从本地硬盘文件上传到FTP服务器上传。

private void UploadFileToFTP() 
{ 
    FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create("ftp://www.server.com/sample.txt"); 

    ftpReq.UseBinary = true; 
    ftpReq.Method = WebRequestMethods.Ftp.UploadFile; 
    ftpReq.Credentials = new NetworkCredential("user", "pass"); 

    byte[] b = File.ReadAllBytes(@"E:\sample.txt"); 
    ftpReq.ContentLength = b.Length; 
    using (Stream s = ftpReq.GetRequestStream()) 
    { 
     s.Write(b, 0, b.Length); 
    } 

    FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse(); 

    if (ftpResp != null) 
    { 
     if(ftpResp.StatusDescription.StartsWith("226")) 
     { 
       Console.WriteLine("File Uploaded."); 
     } 
    } 
} 
相关问题