2014-09-30 4 views
0

내 응용 프로그램에서 사용자에게 PDF 파일을 다운로드 할 수있는 옵션을 제공하고 싶습니다. 내 코드에서 파일은 브라우저에서 열립니다. 그러나 파일을 다운로드하려고합니다. 내가 드롭 다운 메뉴에서 사용하고 방법은 다음과PDF 파일 다운로드 C#

컨트롤러

 string name = id; //id is the name of the file 
     string contentType = "application/pdf"; 

     var files = objData.GetFiles(); //list of files 


     string filename = (from f in files 
          orderby f.DateEncrypted descending 
          where f.FileName == name 
          select f.FilePath).First(); //gets the location of the file 

     string FullName = (from f in files 
          where f.FileName == name 
          select f.FileName).First(); //gets the new id in new location to save the file with that name 



     //Parameters to File are 
     //1. The File Path on the File Server 
     //2. The content type MIME type 
     //3. The parameter for the file save by the browser 
     return File(filename, contentType, FullName); 

: 여기에 내 코드입니다.

보기 :

<li><a id="copyURL" href="@Url.Action("Download", "Home", new { id = item.FileName})">Download</a></li> 

"다운로드"를 클릭하여이 파일은 브라우저를 열어됩니다.

답변

0

콘텐츠 유형을 "application/octet-stream"으로 설정하면 PDF 플러그인이 콘텐츠 유형을 선택하여 표시하지 않습니다. 그런 다음 브라우저가 파일 다운로드로 처리합니다. 웹에서

+0

브라우저가 계속 열려고합니다. 나는 크롬과 IE로 그것을 시도했다. – user3853986

+0

return 문 앞에이 줄을 추가해 볼 수 있습니까? 'Response.AddHeader ("content-disposition", "attachment; filename ="+ filename); ' –

+0

여전히 동일합니다. 나는 시도했다 Response.AddHeader ("content-disposition", "attachment; filename ="+ filename); 및 Response.AddHeader ("content-disposition", "attachment; filename ="+ FullName); – user3853986

0

다운로드 파일 :

이 예는 로컬 디스크에 어떤 웹 사이트에서 파일을 다운로드하는 방법을 보여줍니다. 파일을 다운로드하는 간단한 방법은 WebClient 클래스와 그 메소드 DownloadFile을 사용하는 것입니다. 이 메소드에는 두 개의 매개 변수가 있습니다. 첫 번째는 다운로드 할 파일의 URL이고 두 번째 매개 변수는 파일을 저장할 로컬 디스크의 경로입니다. 파일 다운로드 동 기적으로

다음 코드는 동 기적으로 파일을 다운로드하는 방법을 보여줍니다. 이 메소드는 파일이 다운로드되거나 오류가 발생할 때까지 주 스레드를 차단합니다 (이 경우 WebException이 발생합니다). [C 번호] :

using System.Net; 

WebClient webClient = new WebClient(); 
webClient.DownloadFile("pdf file address", @"c:\myfile.pdf"); 

파일 다운로드가 비동기 : 는 메인 스레드 사용 비동기 방식 DownloadFileAsync을 차단하지 않고 파일을 다운로드 할 수 있습니다. 진행률을 표시하고 파일이 다운로드되었음을 감지하도록 이벤트 핸들러를 설정할 수도 있습니다. [C 번호] :

private void btnDownload_Click(object sender, EventArgs e) 
{ 
    WebClient webClient = new WebClient(); 
    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
    webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 
    webClient.DownloadFileAsync(new Uri("pdf file address"), @"c:\myfile.pdf"); 
} 

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
{ 
    progressBar.Value = e.ProgressPercentage; 
} 

private void Completed(object sender, AsyncCompletedEventArgs e) 
{ 
    MessageBox.Show("Download completed!"); 
} 

심판 : 당신이하지에 지정하지 않는 http://www.csharp-examples.net/download-files/

0

브라우저가 파일을 표시하려고합니다.

File을 보내기 전에 ContentDisposition을 추가하십시오.

var cd = new System.Net.Mime.ContentDisposition 
    { 
     FileName = filename, 
     Inline = false, 
    }; 
Response.AppendHeader("Content-Disposition", cd.ToString()); 
return File(filename, contentType, FullName);