2012-06-26 5 views
0

사용자가 다른 웹 사이트에 로그인 한 다음 해당 서버에서 지정된 파일을 다운로드 할 수있게 해주는이 응용 프로그램에서 작업했습니다. 지금까지 웹 사이트에 로그인하고 파일을 다운로드하는 데 성공했습니다. 그러나 Zip 파일은 모든 것이 파괴됩니다.C에서 리더를 사용하여 zip 파일 다운로드 #

바이트 단위로 .zip 파일을 읽거나 스트림 판독기를 사용하여 도움이 될 수있는 코드가 있습니까?

downloadfile()을 사용하고 있지만 올바른 zip 파일을 반환하지 않았습니다.

zip 파일을 읽을 수있는 방법이 필요합니다. 내가 ByteReader()

zip 파일을 다운로드하는 데 사용되는 코드를 사용하여 그것을 할 수있는 것은 사전에

string filename = "13572_BranchInformationReport_2012-05-22.zip"; 
      string filepath = "C:\\Documents and Settings\\user\\Desktop\\" + filename.ToString(); 
      WebClient client = new WebClient(); 
      string user = "abcd", pass = "password"; 
      client.Credentials = new NetworkCredential(user, pass); 
      client.Encoding = System.Text.Encoding.UTF8; 
      try 
      { 
       client.DownloadFile("https://web.site/archive/13572_BranchInformationReport_2012-05-22.zip", filepath); 
       Response.Write("Success"); 
      } 
      catch (Exception ue) 
      { 
       Response.Write(ue.Message); 
      } 

감사합니다.

+1

이 질문은 명확하지 않습니다. 네가 가진 문제는 뭐니? 문제가있는 코드를 포함하도록 질문을 편집 할 수 있다면 매우 유용 할 것입니다. – Polynomial

+0

편집 된 질문보기 – akhil

+0

아무도 아이디어가있는 경우를 대비하여 질문은 계속됩니다. – akhil

답변

0

답변을 downlaod하는 또 다른 간단한 방법은

WebRequest objRequest = System.Net.HttpWebRequest.Create(url); 
objResponse = objRequest.GetResponse(); 
byte[] buffer = new byte[32768]; 
using (Stream input = objResponse.GetResponseStream()) 
{ 
using (FileStream output = new FileStream ("test.doc", 
FileMode.CreateNew)) 
{ 
int bytesRead; 

while ((bytesRead=input.Read (buffer, 0, buffer.Length)) > 0) 
{ 
output.Write(buffer, 0, bytesRead); 
} 
} 
} 

이것은 내가 그것을 달성하는 방법이다. 모두에게 도움을 주셔서 감사합니다

4

에는 스트림 판독기를 사용하여 zip 파일 바이트를 바이트 단위로 읽는 데 도움이 될 수있는 코드가 있습니다.

절대적으로 아닙니다. StreamReader - 실제로는 TextReader텍스트 콘텐츠의 경우 이 아닌 콘텐츠를 읽는 것입니다. zip 파일은 텍스트가 아니며 문자가 아닌 바이트로 구성됩니다.

zip 파일과 같은 바이너리 콘텐츠를 읽는 경우 TextReader이 아닌 Stream을 사용해야합니다.

일반적으로 WebClient.DownloadFileWebClient.DownloadData은 바이너리 콘텐츠를 다운로드하기가 더 쉽습니다.

+0

나는 이미'WebClient.DownloadFile'을 사용했지만 아무 것도 사용하지 않았다. – akhil

+1

어떤 방법으로 사용하지 않겠습니까? 예외가 발생합니까? 잘못된 데이터를 제공합니까? – Polynomial

+0

@akhil : 음,'WebClient.DownloadFile' *가 작동하기 때문에 문제는 다른 곳에서 발생합니다. 그러나, 당신은 * 우리에게 * 당신에게 더 많은 정보를 제공하지 않았습니다. http://tinyurl.com/so-hints –

0

zip 파일

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/DOWNLOAD/Filename.zip">Click To Download</asp:HyperLink> 

또 다른 해결책

private void DownloadFile() 
    { 


     string getPath = "DOWNLOAD/FileName.zip"; 
     System.IO.Stream iStream = null; 

     byte[] buffer = new Byte[1024]; 

     // Length of the file: 
     int length; 

     // Total bytes to read: 
     long dataToRead; 

     // Identify the file to download including its path. 
     string filepath = Server.MapPath(getPath); 

     // Identify the file name. 
     string filename = System.IO.Path.GetFileName(filepath); 
     try 
     { 
      // Open the file. 
      iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, 
         System.IO.FileAccess.Read, System.IO.FileShare.Read); 


      // Total bytes to read: 
      dataToRead = iStream.Length; 
      // Page.Response.ContentType = "application/vnd.android.package-archive"; 
      // Page.Response.ContentType = "application/octet-stream"; 
      Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); 

      // Read the bytes. 
      while (dataToRead > 0) 
      { 
       // Verify that the client is connected. 
       if (Response.IsClientConnected) 
       { 
        // Read the data in buffer. 
        length = iStream.Read(buffer, 0, 1024); 

        // Write the data to the current output stream. 
        Page.Response.OutputStream.Write(buffer, 0, length); 

        // Flush the data to the HTML output. 
        Page.Response.Flush(); 

        // buffer = new Byte[1024]; 
        dataToRead = dataToRead - length; 
       } 
       else 
       { 
        //prevent infinite loop if user disconnects 
        dataToRead = -1; 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      // Trap the error, if any. 
      Page.Response.Write(ex.Message); 
     } 
     finally 
     { 
      if (iStream != null) 
      { 
       //Close the file. 
       iStream.Close(); 
       Page.Response.Close(); 
      } 

     } 
    } 
+0

내 파일이 원격 서버에 있음 https://web.site/abc.zip로 URL을 보내십시오 – akhil

+0

@akhil : getPath = "web.site/abc.zip"을 설정해야합니다 –

+0

나는 그것을 시도했지만 오류를 반환합니다 * * 주어진 경로의 형식은 지원되지 않습니다. ** – akhil