نحوه ذخیره فایل های pdf,word,excel در دیتابیس
در این مقاله قصد داریم به نحوه ذخیره فایل های pdf,word,excel در دیتابیس با استفاده از asp.net بپردازیم.
برای ذخیره فایل ها کاربران یکی از دو روش زیر را انتخاب می کنند :
1- ذخیره اسم فایل در دیتابیس
2-ذخیره فایل در دیتابیس
روش اول که کار سخت و دشواری نمی باشد فقط کافیست که اسم فایل مربوطه را بعد از آپلود در سایت گرفته و در دیتابیس ذخیره نمایید .هر کدام از دو روش فوق معایب و محاسنی دارند که سعی میکنیم در آینده در این زمینه مقاله ای در اختیار دوستان قرار بدهیم .
مراحلی که در این مقاله طی می شود :
1- ساخت یک جدول جدید برای نگه داری فایل های آپلود شده.
2- ساخت یک پروژه جدید در asp.net .
3- نوشتن کدهای مورد نظر برای رسیدن به مقصود .
مرحله 1:
ساخت یک جدول جدید به اسم Documents که متشکل از فیلدهای زیر می باشد:
- SerialNumber
- FileName
- DisplayName
- Extension
- ContentType
- FileData
- FileSize
- UploadDate
مرحله 2:
پروژه جدید را به اسم FilesToBinary ساخته و یک صفحه جدید به آن اضافه کنید(Conversion.aspx) :
- TextBox
- FileUpload
- Button
1 2 3 4 5 6 7 8 |
<asp:TextBox ID="txtfilename" runat="server"> </asp:TextBox> <br /> Select File <asp:FileUpload ID="FileUpload1" runat="server" /> <br /> <asp:Button ID="Button1" runat="server" Text="Convert" OnClick="Button1_Click" /> |
کدهای زیر مربوط به دکمه button برای ذخیره عکس در دیتابیس می باشد .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
protected void Button1_Click(object sender, EventArgs e) { if (!FileUpload1.HasFile) { Response.Write("No file Selected"); return; } else { string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); string extension = Path.GetExtension(filename); string contentType = FileUpload1.PostedFile.ContentType; HttpPostedFile file = FileUpload1.PostedFile; byte[] document = new byte[file.ContentLength]; file.InputStream.Read(document, 0, file.ContentLength); //Validations if ((extension == ".pdf") || (extension == ".doc") || (extension == ".docx") || (extension == ".xls"))//extension { if (file.ContentLength <= 31457280)//size { //Insert the Data in the Table using (SqlConnection connection = new SqlConnection()) { connection.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ToString(); connection.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = connection; string commandText = @"insert into Documents(Name_File,DisplayName,Extension,ContentType,FileData,FileSize,UploadDate) values(@Name_File,@DisplayName,@Extension,@ContentType,@FileData,@FileSize,getdate())"; cmd.CommandText = commandText; cmd.CommandType = CommandType.Text; cmd.Parameters.Add("@Name_File", SqlDbType.VarChar); cmd.Parameters["@Name_File"].Value = filename; cmd.Parameters.Add("@DisplayName", SqlDbType.VarChar); cmd.Parameters["@DisplayName"].Value = txtfilename.Text.Trim(); cmd.Parameters.Add("@Extension", SqlDbType.VarChar); cmd.Parameters["@Extension"].Value = extension; cmd.Parameters.Add("@ContentType", SqlDbType.VarChar); cmd.Parameters["@ContentType"].Value = contentType; cmd.Parameters.Add("@FileData", SqlDbType.VarBinary); cmd.Parameters["@FileData"].Value = document; cmd.Parameters.Add("@FileSize", SqlDbType.BigInt); cmd.Parameters["@FileSize"].Value = document.Length; cmd.ExecuteNonQuery(); cmd.Dispose(); connection.Close(); Response.Write("Data has been Added"); } } else { Response.Write("Inavlid File size"); return; } } else { Response.Write("Inavlid File"); return; } } } |
عکس زیر نمایشی از جدول Documents می باشد :
نحوه خواندن فایل های ذخیره شده در دیتابیس:
جهت خواندن فایل های ذخیره شده در دیتابیس و نمایش آنها به کاربران می توانید متدی شبیه به متد زیر را استفاده کنید :
ابتدا دستور select مورد نظر خود را نوشته و بعد از واکشی می توانید DataTable فوق را به متد مورد نظر پاس دهید . می توانید پارامتر ورودی متد زیر را متناسب با نیاز خود تغییر دهید .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
private void download (DataTable dt) { Byte[] bytes = (Byte[])dt.Rows[0]["FileData"]; Response.Buffer = true; Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = dt.Rows[0]["ContentType"].ToString(); Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Name_file"].ToString()); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } |
دیدگاهتان را بنویسید
برای نوشتن دیدگاه باید وارد بشوید.