2011-12-21 40 views
0

我已經完成了一項任務,可以使用日曆和下載形式的FTP來選擇csv文件。保存到本地驅動器

目前我已經預定義了保存csv文件的本地驅動器路徑。

我需要顯示彈出保存,然後用戶必須保存到任何位置,因爲他們需要。

private void Download() 
{ 
    System.DateTime myDate = new System.DateTime(); 
    myDate = caldownload.SelectedDate; 
    System.DateTime myDate1 = new System.DateTime(); 
    myDate1 = Calendar1.SelectedDate; 
    string sdate; 
    string edate; 
    TextBox1.Text = myDate.ToString("yyyy-MM-dd"); 
    TextBox2.Text = myDate1.ToString("yyyy-MM-dd"); 
    DateTime d1 = myDate.Date; 
    DateTime d2 = myDate1.Date; 
    TimeSpan sum = d2 - d1; 

    int NrOfDays = sum.Days; 
    NrOfDays++; 

    int k = NrOfDays; 
    string[] fileName = new string[k]; 

    //increment for days 
    int s = myDate.Day; 

    FtpWebRequest reqFTP; 
    try 
    { 
    if (NrOfDays<=7) 
    { 
     for (k = 0; k <= NrOfDays; k++) 
     { 
     fname[i] = myDate.ToString("yyyy-MM-dd"); 
     fileName[k] = myDate.ToString("yyyy-MM-dd"); 

     //files to save local drive 
     FileStream outputStream = new FileStream("F:\\ch" + "\\" + fname[i]+".csv", FileMode.Create); 
     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + "192.162.1.152" + "//" + "[100]" + "//" + fileName[k] + ".csv")); 

     reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 
     reqFTP.Method = WebRequestMethods.Ftp.DownloadFile; 
     reqFTP.UseBinary = true; 
     reqFTP.Credentials = new NetworkCredential("", ""); 
     FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 

     Stream ftpStream = response.GetResponseStream(); 
     long cl = response.ContentLength; 
     int bufferSize = 2048; 
     int readCount; 
     byte[] buffer = new byte[bufferSize]; 

     readCount = ftpStream.Read(buffer, 0, bufferSize); 
     while (readCount > 0) 
     { 
      outputStream.Write(buffer, 0, readCount); 
      readCount = ftpStream.Read(buffer, 0, bufferSize); 
     } 

     myDate = myDate.AddDays(1); 

     ftpStream.Close(); 
     outputStream.Close(); 
     response.Close(); 
     } 
    } 
    else 
    { 
     ScriptManager.RegisterStartupScript(this, this.GetType(), "message", "alert('You are allowed to download files for maximum of 7 days .');location.href = 'Default.aspx';", true); 
    } 
    } 
    catch (Exception ex) 
    { } 
} 
+0

是用戶從服務器下載到自己的客戶機上,或者某處將其保存在服務器上? – 2011-12-21 04:17:27

回答

2

您可以添加一個SaveFileDialog以從用戶檢索文件的名稱。

例如:

 string sFileNameToSaveAs = ""; 

     using (var dialog = new SaveFileDialog()) 
     { 
      dialog.AddExtension = true; 
      dialog.Filter = "CSV Files (*.csv) | *.csv"; 
      dialog.Title = "Select the file name to save as"; 
      dialog.InitialDirectory = "C:\\"; 
      dialog.CheckPathExists = true; 
      dialog.DefaultExt = ".csv"; 
      dialog.ValidateNames = true; 

      if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
      { 
       sFileNameToSaveAs = dialog.FileName; 
      } 
     } 

     if (!string.IsNullOrEmpty(sFileNameToSaveAs)) 
     { 
      FileStream outputStream = new FileStream(sFileNameToSaveAs, FileMode.Create); 
      // The rest of your retrieval code goes here 
     } 
+0

感謝您的答覆..使用在網絡不是Windows :(任何其他解決方案? – user1109068 2011-12-21 06:00:13

+0

http://stackoverflow.com/questions/8299096/browse-directories-for-save-location,檢查此鏈接,作爲其關注的安全,你不能這樣做 – 2011-12-21 07:35:17

+0

感謝您的答覆..然後我該怎麼辦?我需要選擇多個文件的開始和結束日期,並下載並保存到客戶端機器..幫助我 – user1109068 2011-12-22 07:31:10

相關問題