2011-12-21 3 views
0

캘린더를 사용하여 csv 파일을 선택하고 FTP를 사용하여 다운로드 할 수있는 작업을 수행했습니다.로컬 드라이브에 저장

현재 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

가 사용자 자신의 클라이언트 시스템에 서버에서 다운로드, 또는 서버에 어딘가에 저장? –

답변

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

+0

http://stackoverflow.com/questions/8299096/browse-directories-for-save-location, 해당 링크를 확인하십시오. 보안, 당신은 할 수 없어. –

+0

답장을 보내 주셔서 감사합니다 .. 그럼 어떻게 할 수 있습니까? 시작 및 종료 날짜를 하나 이상의 파일을 선택하고 다운로드하여 클라이언트 컴퓨터에 저장해야합니다. – user1109068

관련 문제