2016-09-22 2 views
0

ASP.NET에서 내 데이터베이스에서 파일을 다운로드하는 스크립트를 만듭니다. 다운로드 버튼을 클릭하면 다운로드로드 파일은 내 다운로드 버튼의 페이지 이름과 함께 제목이되며 파일을 업로드 할 때 원래 저장했던 .docx 파일 대신 .aspx 파일로 저장됩니다.ASP.NET 다운로드 스크립트가 현재 페이지 파일을 다운로드 파일로로드 중입니다.

이 문제를 해결하려면 지원이 필요합니다.

protected void Download(object sender, EventArgs e) 
{ 
    if (Request.QueryString["Request-Id"] != "") 
    { 
     string fileName = getFileName(Convert.ToInt32(Request.QueryString["Request-Id"])); 

     if (fileName != "") { 
      Response.ContentType = "Application/vnd.openxmlformats-officedocument.wordprocessingml.document"; 
      Response.AppendHeader("content-deposition", "attachment/filename=\""+fileName+"\""); 
      Response.TransmitFile(Server.MapPath("/Uploads/" + fileName)); 
      Response.End(); 
     } 
    } 
} 

protected string getFileName(int id) 
{ 
    string connStr = ConfigurationManager.ConnectionStrings["DbCall"].ToString(); 
    SqlConnection conn = new SqlConnection(connStr); 

    conn.Open(); 
    SqlCommand command = new SqlCommand("select Conveyance_Document from Convey where Request_Id = @Id", conn); 
    command.Parameters.AddWithValue("@Id", Convert.ToInt32(id)); 

    SqlDataReader sdr = command.ExecuteReader(); 

    if (sdr.Read()) 
    { 
     string fileName = sdr["Conveyance_Document"].ToString(); 
     conn.Close(); 
     conn.Dispose(); 
     return fileName; 
    } 
    else 
    { 
     conn.Close(); 
     conn.Dispose(); 
     return ""; 
    } 
} 
+2

* 디버깅 도움말 ("이 코드가 작동하지 않는 이유는 무엇입니까?")에는 원하는 동작, 특정 문제 또는 오류 및 질문 자체에서 재현하는 데 필요한 가장 짧은 코드가 포함되어야합니다. 명확한 문제 성명이없는 질문은 다른 독자에게 유용하지 않습니다. * [mcve] –

+0

이 질문은 이미 많은 질문을 받았기 때문에 쉽게 대답 할 수있었습니다. http://stackoverflow.com/search?q=how+ to + download + file + asp.net –

답변

0

이 시도 :

Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; 
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); 
Response.TransmitFile(Server.MapPath("/Uploads/" + fileName)); 
Response.End(); 

헤더 이름 Content-Disposition하지 content-deposition입니다.

+0

고마워, 제대로 작동하고있어. 제발 좀 도와 줄 수있어. 제발. –

+0

네, 도와 드리겠습니다. 그러나 문제가 해결되면 제 대답을 확인하십시오. 다른 문제가있는 경우 다른 질문을 추가하십시오. – krlzlx

+0

제발 당신이 asp.net에서 내 웹 서비스의 사용자 응답을 확인하려면 내 응용 프로그램의 백그라운드에서 실행됩니다 필요합니다. 이것은 그것이 어떻게 진행되는지입니다. 일부가 로그를 입력하면 로그는 특정 부서의 응답이 필요합니다. 그래서 로그가 부서에 의해 응답되었는지 확인하기 위해 백그라운드 검사를해야합니다. 24 시간이 지나지 않은 경우, 다른 부서에 전자 메일을 보내어 회신하도록 상기시켜야합니다. 내 문제는 검사를 수행 할 백그라운드 서비스를 만드는 방법을 모르겠다. –

관련 문제