如何在数据库中存储文件 [英] How to store a file in database
本文介绍了如何在数据库中存储文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在asp.net中保存文件时遇到问题...我正在使用三层架构存储文件
受保护 void save_Click( object sender,EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString [ ID]))
{
int ID = Convert.ToInt32(Request.QueryString [ ID]);
if (FileUpload1.HasFile)
{
string FileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (FileExtension.ToLower()!= .doc&& FileExtension.ToLower()!= 。docx&& FileExtension.ToLower()!= 。pdf)
{
Label1.Text = 仅允许.doc或.docx或.pdf文件;
}
else
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
使用(Stream fs = FileUpload1.PostedFile.InputStream)
{
使用(BinaryReader br = new BinaryReader(fs))
{
byte [] bytes = br.ReadBytes(( Int32 )fs.Length);
}
}
}
if (IITBAL.Assignments.UpdateFacultyAssignment(ID,SessionId.ToString(),Semester.SelectedItem.Value.ToString(),Course_Name.SelectedItem.Value.ToString(),FileUpload1.FileName.ToString() ,date.Text.ToString()))
{
Response.Redirect( Faculty Manage Assignments.aspx跨度>);
}
else
{
Label1.Text = 无法添加分配;
}
}
}
其他
{
if (FileUpload1.HasFile)
{
string FileExtension = System.IO .Path.GetExtension(FileUpload1.FileName);
if (FileExtension.ToLower()!= .doc&& FileExtension.ToLower()!= 。docx&& FileExtension.ToLower()!= 。pdf)
{
Label1.Text = 仅允许.doc或.docx或.pdf文件;
}
else
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
使用(Stream fs = FileUpload1.PostedFile.InputStream)
{
使用(BinaryReader br = new BinaryReader(fs))
{
byte [] bytes = br.ReadBytes(( Int32 )fs.Length);
}
}
}
如果(IITBAL.Assignments.AddFacultyNewAssignment(SessionId.ToString(),
Semester.SelectedItem.Value.ToString(),Course_Name.SelectedItem.Value.ToString(),UploadFile.FileName.ToString(),date .Text.ToString()))
// IITBAL.Assignment.AddFacultyNewAssignment mrthod如下所述
{
Response.Redirect( Faculty Manage Assignments.aspx) ;
}
else
{
Label1.Text = 无法添加作业;
}
}
}
}
public static bool AddFacultyNewAssignment( string SessionId, string 学期, string CourseName,字符串文件,字符串日期)
{
// 教师添加新作业时的查询
string qtext = string .Format( INSERT INTO TblFacultyAssignment( FacultyId,Semester,CourseName,SelectedFile,Date)VALUES('{0}','{1}','{2}','{3}','{4}'),SessionId,学期,课程名称,档案,日期);
return IITDAL.DAL.ExecuteQuerysql(qtext)> 0 跨度>;
}
解决方案
1.用于存储文件的数据库字段必须是类型的图像
,如果你使用的是MS SQL。
2.所以你应该把文件内容读作字节数组(byte []
然后将字节保存到SQLImage
字段中,如下例所示: http://msdn.microsoft.com/en-us/library/4f5s1we0(v = vs.110)的.aspx [ ^
I have a problem when i want to save a file in asp.net...I am using three tier architecture ti store the file
protected void save_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
{
int ID = Convert.ToInt32(Request.QueryString["ID"]);
if (FileUpload1.HasFile)
{
string FileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (FileExtension.ToLower() != ".doc" && FileExtension.ToLower() != ".docx" && FileExtension.ToLower() != ".pdf")
{
Label1.Text = "Only .doc or .docx or .pdf Files are allowed";
}
else
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
}
}
}
if (IITBAL.Assignments.UpdateFacultyAssignment(ID, SessionId.ToString(), Semester.SelectedItem.Value.ToString(), Course_Name.SelectedItem.Value.ToString(), FileUpload1.FileName.ToString(), date.Text.ToString()))
{
Response.Redirect("Faculty Manage Assignments.aspx");
}
else
{
Label1.Text = "Unable to Add Assignment";
}
}
}
else
{
if (FileUpload1.HasFile)
{
string FileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (FileExtension.ToLower() != ".doc" && FileExtension.ToLower() != ".docx" && FileExtension.ToLower() != ".pdf")
{
Label1.Text = "Only .doc or .docx or .pdf Files are allowed";
}
else
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
}
}
}
if (IITBAL.Assignments.AddFacultyNewAssignment(SessionId.ToString(),
Semester.SelectedItem.Value.ToString(), Course_Name.SelectedItem.Value.ToString(),UploadFile.FileName.ToString(), date.Text.ToString()))
// IITBAL.Assignment.AddFacultyNewAssignment mrthod is described below
{
Response.Redirect("Faculty Manage Assignments.aspx");
}
else
{
Label1.Text = "Unable to add Assignment";
}
}
}
}
public static bool AddFacultyNewAssignment(string SessionId, string Semester, string CourseName, string file,string date)
{
// query when faculty add new assignment
string qtext = string.Format("INSERT INTO TblFacultyAssignment(FacultyId,Semester,CourseName,SelectedFile,Date) VALUES ('{0}','{1}','{2}','{3}','{4}')", SessionId, Semester, CourseName, file,date);
return IITDAL.DAL.ExecuteQuerysql(qtext) > 0;
}
解决方案
1.The database field used to store the files must be of typeImage
, if you are using MS SQL.
2.So you should read the file content as bytes arrays (byte[]
then send save the bytes into the SQLImage
field, like in the next example: http://msdn.microsoft.com/en-us/library/4f5s1we0(v=vs.110).aspx[^]
这篇关于如何在数据库中存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文