기본적으로 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();
}
}
}
}
저장되면 'FileName' 필드를 사용하여 파일 확장명을 사용하고 있습니까? – DonBoitnott
아니요. @Type을 사용하여 다음과 같이 확장명을 가져옵니다. cmd.Parameters.AddWithValue ("@ Type", DocumentsUploadControl.PostedFile.ContentType.ToString()); –