ASP.NET怎么上传图片并保存到数据库中去 有一个上传按钮和一个图片控件,我想点上传就跳出一个选择图片的对话框,然后在图片控件中显示,然后点提交就保存到数据库中去,该怎么实现呢,请大家给点帮忙.是ASP.NET中的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 数据库都支持BLOB数据类型,把流insert到这个字段即可。 fileupload,保存路径到数据库if (fileUpload.HasFile) { string savePath = Server.MapPath("~/upload/"); if (!System.IO.Directory.Exists(savePath)) { System.IO.Directory.CreateDirectory(savePath); } savePath = savePath + "\\" + fileUpload.FileName; fileUpload.SaveAs(savePath);//保存文件 } <script language="javascript" type="text/javascript"> function PreviewImg(imgFile) { var newPreview = document.getElementById("newPreview"); newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value; newPreview.style.width = "80px"; newPreview.style.height = "60px"; } </script> <asp:FileUpload ID="FileUpload1" runat="server" onchange="PreviewImg(this)" /> <div id="newPreview"> </div> function $(o){return document.getElementById(o);} function CheckImg(o,img) { if (!/\.((jpg)|(bmp)|(gif)|(png))$/ig.test(o.value)) { alert('只能上传jpg,bmp,gif,png格式图片!'); o.outerHTML = o.outerHTML; } else { $(img).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src=o.value; } } <asp:FileUpload ID="FileUpload1" runat="server" onchange="CheckImg(this, 'img');" /> <div id="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src= <%= pic%>,sizingMethod=scale);width:88px;height:113px;"> </div> public string pic=""; 我前不久也看到这样的文章,特意把它从我的博客里为你找出来private void button1_Clickobject sender, EventArgs e) { openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP"; if(openFileDialog1.ShowDialog()==DialogResult.OK) { string fullpath =openFileDialog1.FileName;//文件路径 FileStream fs = new FileStream(fullpath, FileMode.Open); byte[] imagebytes =new byte[fs.Length]; BinaryReader br = new BinaryReader(fs); imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length)); //数据库连接 SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05"); con.Open(); SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con); com.Parameters.Add("ImageList", SqlDbType.Image); com.Parameters["ImageList"].Value = imagebytes; com.ExecuteNonQuery(); con.Close(); } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Csdbfans/archive/2010/06/19/5679611.aspx 其中openFileDialog1就是那个上传控件的ID http://www.17aspx.com/html/aspnet/file/2009/0930/87.html 想要可以用的源代码吗?你懂的 [email protected] 直接将文件路径放到页面的一个链接上,比如<a href="路径">,到时候点击就可以下载了 视频文件变成一张张的图片 字符串的连接 求C#调用打印机和调用PRODEDURE的工具类 url传递加密的参数出现错误 文本框光标问题 要四了!救命呀!高手,大侠快来! socket客户端如何接收服务端处理完返回的信息 向缓存文件夹存数据和向文件夹存数据的区别 关于菜单与FOR 结个鸟贴!!老是脚本运行错误,根本无法结。散分 VS2010创建的WCF Web Service第一次调用很慢的问题!!! MDI主窗体打开后总是自动最小化到任务栏
if (fileUpload.HasFile)
{
string savePath = Server.MapPath("~/upload/");
if (!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
savePath = savePath + "\\" + fileUpload.FileName;
fileUpload.SaveAs(savePath);//保存文件
}
<script language="javascript" type="text/javascript">
function PreviewImg(imgFile)
{
var newPreview = document.getElementById("newPreview");
newPreview.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgFile.value;
newPreview.style.width = "80px";
newPreview.style.height = "60px";
}
</script>
<asp:FileUpload ID="FileUpload1" runat="server" onchange="PreviewImg(this)" />
<div id="newPreview"> </div>
function $(o){return document.getElementById(o);}
function CheckImg(o,img)
{
if (!/\.((jpg)|(bmp)|(gif)|(png))$/ig.test(o.value))
{
alert('只能上传jpg,bmp,gif,png格式图片!');
o.outerHTML = o.outerHTML;
}
else
{
$(img).filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src=o.value;
}
} <asp:FileUpload ID="FileUpload1" runat="server" onchange="CheckImg(this, 'img');" />
<div id="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src= <%= pic%>,sizingMethod=scale);width:88px;height:113px;"> </div>
public string pic="";
{
openFileDialog1.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP";
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
string fullpath =openFileDialog1.FileName;//文件路径
FileStream fs = new FileStream(fullpath, FileMode.Open);
byte[] imagebytes =new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
//数据库连接
SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=db_05");
con.Open();
SqlCommand com = new SqlCommand("insert into tb_08 values(@ImageList)",con);
com.Parameters.Add("ImageList", SqlDbType.Image);
com.Parameters["ImageList"].Value = imagebytes;
com.ExecuteNonQuery();
con.Close();
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/Csdbfans/archive/2010/06/19/5679611.aspx