2011-01-10 7 views
4

pdfファイルのダウンロードに問題があります。他のファイルはダウンロードされます。 コード:uはC#でPDFファイルをダウンロードするコード

+7

問題は何ですか? –

+0

私の問題は、上記のコードを使ってpdfファイルをダウンロードしてpdfドキュメントとして保存した後です。私はそれを開くしようとすると、ドキュメントの修復エラーを示しています。 –

+0

私は...あなたを助けるでしょう」リンクの下にこれを期待し http://stackoverflow.com/questions/10829168/can-we-use-response-flush-instead-of-response-end/17038408 #17038408 – Viishnuu

答えて

8

チェックこの方法を知っていれば

WebClient client = new WebClient(); 
client.DownloadFile(remoteFilename, localFilename); 

のplsは私を助けて、希望

 public static void DownloadFile(HttpResponse response,string fileRelativePath) 
    { 
     try 
     { 
      string contentType = ""; 
      //Get the physical path to the file. 
      string FilePath = HttpContext.Current.Server.MapPath(fileRelativePath); 

      string fileExt = Path.GetExtension(fileRelativePath).Split('.')[1].ToLower(); 

      if (fileExt == "pdf") 
      { 
       //Set the appropriate ContentType. 
       contentType = "Application/pdf"; 
      } 

      //Set the appropriate ContentType. 
      response.ContentType = contentType; 
      response.AppendHeader("content-disposition", "attachment; filename=" + (new FileInfo(fileRelativePath)).Name); 

      //Write the file directly to the HTTP content output stream. 
      response.WriteFile(FilePath); 
      response.End(); 
     } 
     catch 
     { 
      //To Do 
     } 
    } 
+1

あなたはcontentType = "Application/pdf"を設定する必要があります。ダウンロードしたファイルが正しいことを確認するPDFファイル –

+0

ありがとうMahmoud私はこれを試しましたが、次のエラーが表示されます:型または名前空間の名前 'HttpResponse'が見つかりませんでした(使用するディレクティブまたはアセンブリ参照がありません) –

+0

あなたはWebアプリケーションやデスクトップアプリケーションで作業していますか? –

1

@Syed Mudhasir役立ちます: そのちょうどライン:)

client.DownloadFileAsync(new Uri(remoteFilename, UriKind.Absolute), localFilename); 

pdfファイルをダウンロードします。 :)

3

次のコードサンプルをダウンロードしてください.pdfファイルをダウンロードしてください。

Response.ContentType = "Application/pdf"; 
Response.AppendHeader("Content-Disposition", "attachment; filename=Test_PDF.pdf"); 
Response.TransmitFile(Server.MapPath("~/Files/Test_PDF.pdf")); 
Response.End(); 
-1

これが私の仕事:

string strURL = @"http://192.168.1.xxx/" + sDocumento; 
WebClient req = new WebClient(); 
HttpResponse response = HttpContext.Current.Response; 
response.Clear(); 
response.ClearContent(); 
response.ClearHeaders(); 
response.Buffer = true; 
response.AddHeader("Content-Disposition", "attachment;filename=\"" + sDocumento + "\""); 
byte[] data = req.DownloadData(strURL); 
response.BinaryWrite(data); 
response.End(); 
関連する問題