2011-08-31 53 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."); 
     } 
    } 
} 
相關問題