2013-06-21 2 views
2

오케이,파일이 올바른 형식으로 다운로드되지 않습니다.

기본적으로 asp.net 및 C#을 사용하여 SQL 서버 데이터베이스에 파일을 업로드 할 수있었습니다. 브라우저에서 아무 문제없이 볼 수 있지만 다운로드 할 때 원본 형식을 잃어 버리는 것처럼 보입니다. 워드 문서는 자신의 docx 확장자를 잃어 버리고 수동으로 워드 문서로 열어 선택해야합니다. 여기

지금까지

그것은 파일 이름을 저장 한 값이 텍스트 상자에 무엇을 사용 유형에 (FilenameTB)에 의존 (내가 확실히 말할 수는 없지만)과 같은
//Method used to upload a file to the database 
    protected void UploadBut_Click(object sender, EventArgs e) 
    { 
     Stream inpStream = DocumentsUploadControl.PostedFile.InputStream; 
     BinaryReader br = new BinaryReader(inpStream); 
     Byte[] size = br.ReadBytes ((int)inpStream.Length); 

     using (SqlConnection conn = new SqlConnection("Data Source=conn\\sqlexpress;Initial Catalog=new catalog;Integrated Security=True")) 
     { 

      string sql = "INSERT INTO Attachments(AttachmentReferenceID, AttachmentType, Filename, AttachmentDescription, FileUploadedBy, UploadDate)" + 
       "values (@Reference, @Type, @Filename, @Descr, @UploadedBy, @UploadedDate)"; 

      using (SqlCommand cmd = new SqlCommand(sql, conn)) 
      { 
       cmd.Parameters.AddWithValue("@Reference", ReferenceDDL.Text.Trim()); 
       cmd.Parameters.AddWithValue("@Type", DocumentsUploadControl.PostedFile.ContentType.ToString()); 
       cmd.Parameters.AddWithValue("@Filename", FilenameTB.Text.Trim()); 
       cmd.Parameters.AddWithValue("@Descr", size); 
       cmd.Parameters.AddWithValue("@UploadedBy", Session["username"].ToString()); 
       cmd.Parameters.AddWithValue("@UploadedDate", DateTime.Now.Date.ToShortDateString()); 

       conn.Open(); 
       cmd.ExecuteNonQuery(); 
       conn.Close(); 

       Response.Redirect("Documents.aspx"); 
      } 

     } 

    } 


    //listener used to download the file 
    protected void lnkDownload_Click(object sender, EventArgs e) 
    { 
     LinkButton lnkbtn = sender as LinkButton; 
     GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow; 
     int fileId = Convert.ToInt32(DocumentsGridView.DataKeys[gvrow.RowIndex].Value.ToString()); 

     using (SqlConnection conn = new SqlConnection("Data Source=conn\\sqlexpress;Initial Catalog=new catalog;Integrated Security=True")) 
     { 
      string sql = "SELECT AttachmentReferenceID, AttachmentType, Filename, AttachmentDescription, FileUploadedBy, UploadDate FROM Attachments WHERE [email protected]"; 

      using (SqlCommand cmd = new SqlCommand(sql, conn)) 
      { 
       cmd.Parameters.AddWithValue("@ID", fileId); 
       conn.Open(); 

       SqlDataReader dr = cmd.ExecuteReader(); 

       if (dr.Read()) 
       { 
        Response.ContentType = dr["AttachmentType"].ToString(); 
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["Filename"] + "\""); 
        Response.BinaryWrite((byte[])dr["AttachmentDescription"]); 
        Response.End(); 

        conn.Close(); 
       } 
      } 
     } 
    } 
+0

저장되면 'FileName' 필드를 사용하여 파일 확장명을 사용하고 있습니까? – DonBoitnott

+0

아니요. @Type을 사용하여 다음과 같이 확장명을 가져옵니다. cmd.Parameters.AddWithValue ("@ Type", DocumentsUploadControl.PostedFile.ContentType.ToString()); –

답변

0

당신은 같은 Response.AddHeader 코드에 확장/첨부 파일 유형을 추가해야합니다.

감사합니다.

+0

이것이 효과가 있습니다! 감사합니다. –

+0

도움이 되었기 때문에 기쁩니다. 천만에요. 해피 코딩 ..! :) – Vamsi

0

보다는 내 코드입니다 디스크상의 파일 이름?

PostedFile.FileName을 살펴 본다면 확장 프로그램을 추출 할 수 있다고 생각하십니까?

1

첨부 파일 형식에 application /을 추가해보십시오. 나는.

Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["Filename"] + "." + dr["AttachmentType"].ToString()); 

이 하나를 시도하십시오 :

Response.contenttype = "application/" + dr["attachmenttype"].ToString(); 
관련 문제