各位大哥!文件怎样上传和读取啊! 文件怎样上传到数据库当中啊!我用的是这个控件!和一个按钮!<input id="txtDoc"tabIndex="18" type="file" name="txtDoc" runat="server">然后怎么从数据库当中读取啊!请代码示例!C#语言! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 向数据库中传文件,需要将文件转换为流,(img格式)然后保存,读取的时候也是以流的形式取下,在根据保存的格式,恢复名称(包括扩展称)建议不要上传到数据库中(我都是建立一个文件夹,然后上传到这个文件夹中,读取的时候只要给出路径就可以了,可以下载,也可以打开)给一点简单的上传文件的代码给你:放一个HtmlInputFile在用他的PostedFile.SaveAs属性保存文件就可以了! File1 是服务器段运行的,表(imgtitle,imgtype,imgdata [image类型])Stream imgdatastream = File1.PostedFile.InputStream; int imgdatalen = File1.PostedFile.ContentLength; string imgtype = File1.PostedFile.ContentType; string imgtitle = File1.PostedFile.FileName.Substring (File1.PostedFile.FileName.LastIndexOf('\\'));//取文件名 byte[] imgdata = new byte[imgdatalen];//读成字节 string connstr="user id = sa;password = tianshuang;initial catalog = jsj2004;data source =localhost;Connect timeout = 20"; SqlConnection connection = new SqlConnection(connstr); SqlCommand command = new SqlCommand("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)VALUES ( @imgtitle, @imgtype,@imgdata )", connection ); SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50 ); paramTitle.Value = imgtitle; command.Parameters.Add( paramTitle); SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image ); paramData.Value = imgdata; command.Parameters.Add( paramData ); SqlParameter paramType = new SqlParameter( "@imgtype", SqlDbType.VarChar,50 ); paramType.Value = imgtype; command.Parameters.Add( paramType ); connection.Open(); int numRowsAffected = command.ExecuteNonQuery(); connection.Close(); string fileName=this.File1.PostedFile.FileName; string name=Path.GetFileName(fileName); string exName=Path.GetExtension(fileName); if(exName.ToUpper().Trim()!=".XLS") { Response.Write("<script>window.alert('文件类型不对,只能是Excel文件,请重新选择')</script>"); return; } int fileLength=this.File1.PostedFile.ContentLength/1024; if(fileLength>4096) { Response.Write("<script>window.alert('文件太大了,不能超过四兆')</scrpt>"); return; } string tempFileName="k:\\data\\getup\\"+name; this.File1.PostedFile.SaveAs(tempFileName); int i=0; while(true) { i++; if(File.Exists(tempFileName)) break; if(i>10000) { Response.Write("<script>window.alert('超时,请重新操作')</script>"); return; } } string strConnection="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+tempFileName+";Extended Properties=Excel 8.0"; OleDbConnection eConnection=new OleDbConnection(strConnection); eConnection.Open(); string strSelect="SELECT pc_brnn FROM [Sheet1$]"; OleDbCommand eCommand=new OleDbCommand(); eCommand.Connection=eConnection; eCommand.CommandText=strSelect; OleDbDataReader odr; try { odr=eCommand.ExecuteReader(); } catch(Exception xx) { Response.Write(xx.ToString()); Response.Write("<script>alert('打开Excel文件出错')</script>"); eConnection.Close(); return; } 用httpPostedFile这个类来得到所上传得文件。然后用他的SaveAs方法就可以上传图片了 怎么获取页面控件的值 怎么选择来判断复选框的可选状态 点击小类别列表之后。怎样才能让他不收缩。保持原来展开状态 Session过期问题 怎样发布网站 在SQL数据库中读取到的时间数据为<NULL>,转为时间格式时候出现异常.如何操作才好 发布网站的一个小小疑问!!!!!!!!!!!!!! 如何提升权限?我只记得有人说过改machine.config里的什么东西,但是我忘了 问一个简单问题:html服务器控件在cs文件中的引用 这段属性有什么问题? 如何验证提交信息的来源是否是本页面提交?? 如何在DataGrid中的模版列中的CheckBox实现单选?
建议不要上传到数据库中(我都是建立一个文件夹,然后上传到这个文件夹中,读取的时候只要给出路径就可以了,可以下载,也可以打开)
给一点简单的上传文件的代码给你:
放一个HtmlInputFile
在用他的PostedFile.SaveAs属性保存文件就可以了!
int imgdatalen = File1.PostedFile.ContentLength;
string imgtype = File1.PostedFile.ContentType;
string imgtitle = File1.PostedFile.FileName.Substring (File1.PostedFile.FileName.LastIndexOf('\\'));//取文件名
byte[] imgdata = new byte[imgdatalen];//读成字节
string connstr="user id = sa;password = tianshuang;initial catalog = jsj2004;data source =localhost;Connect timeout = 20"; SqlConnection connection = new SqlConnection(connstr); SqlCommand command = new SqlCommand("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)VALUES ( @imgtitle, @imgtype,@imgdata )", connection ); SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50 ); paramTitle.Value = imgtitle;
command.Parameters.Add( paramTitle); SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
paramData.Value = imgdata;
command.Parameters.Add( paramData ); SqlParameter paramType = new SqlParameter( "@imgtype", SqlDbType.VarChar,50 );
paramType.Value = imgtype;
command.Parameters.Add( paramType ); connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
string name=Path.GetFileName(fileName);
string exName=Path.GetExtension(fileName);
if(exName.ToUpper().Trim()!=".XLS")
{
Response.Write("<script>window.alert('文件类型不对,只能是Excel文件,请重新选择')</script>");
return;
}
int fileLength=this.File1.PostedFile.ContentLength/1024;
if(fileLength>4096)
{
Response.Write("<script>window.alert('文件太大了,不能超过四兆')</scrpt>");
return;
} string tempFileName="k:\\data\\getup\\"+name;
this.File1.PostedFile.SaveAs(tempFileName);
int i=0;
while(true)
{
i++;
if(File.Exists(tempFileName))
break;
if(i>10000)
{
Response.Write("<script>window.alert('超时,请重新操作')</script>");
return;
}
} string strConnection="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+tempFileName+";Extended Properties=Excel 8.0";
OleDbConnection eConnection=new OleDbConnection(strConnection);
eConnection.Open();
string strSelect="SELECT pc_brnn FROM [Sheet1$]";
OleDbCommand eCommand=new OleDbCommand();
eCommand.Connection=eConnection;
eCommand.CommandText=strSelect;
OleDbDataReader odr;
try
{
odr=eCommand.ExecuteReader();
}
catch(Exception xx)
{
Response.Write(xx.ToString());
Response.Write("<script>alert('打开Excel文件出错')</script>");
eConnection.Close();
return;
}