ASP.NET 2.0中怎么上传照片啊?急! 在VS2005开发平台上 怎么利用HTML 中的input(file)控件传照片到数据库啊?别的方法也中~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 private string UploadPicFile(System.Web.UI.HtmlControls.HtmlInputFile Fupload) { //文件上传 try { if (Fupload.PostedFile.FileName == "") return ""; string dir = DateTime.Now.ToString("yyyy"); if (!Directory.Exists(Server.MapPath("uploadpic"))) { Directory.CreateDirectory(Server.MapPath("uploadpic")); if (!Directory.Exists(Server.MapPath("uploadpic"))) return ""; } Random rd = new System.Random(); string filename; string extname; if (Fupload.PostedFile.FileName != "") { extname = Fupload.PostedFile.FileName.Substring(Fupload.PostedFile.FileName.LastIndexOf(".") + 1).ToUpper(); if ("JPG".IndexOf(extname) == -1) { return ""; } filename = dir + "\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + rd.Next(1000).ToString() + "." + extname; string mfileExtension = System.IO.Path.GetExtension(filename); Fupload.PostedFile.SaveAs(Server.MapPath("uploadpic\\") + filename); return "uploadpic\\" + filename; } return ""; } catch { return ""; } } <input id="File2" type="file" runat="server" /> string imgUrl = this.UploadPicFile(this.File1); if (imgUrl == "") { return; } 给你来段代码public static string path; protected void btnGoUp_Click(object sender, EventArgs e) { string str = this.File1.PostedFile.FileName; if (str == "") { Response.Write(bc.MassageBox("请选择上传图片!")); return; } string ss = str.Substring(str.LastIndexOf("\\") + 1); string s = Server.MapPath("~\\photo\\" + ss); path = "~\\photo\\" + ss; if (File.Exists(s)) { Response.Write(bc.MassageBox("该文件已经存在,请重新命名!!!")); return; } this.File1.PostedFile.SaveAs(s); Response.Write(bc.MassageBox("上传成功!")); }存数据库的时候就存path就是当前图片的路径了页面这样<input id="File1" runat="server" class="InputCss" style="width: 287px" type="file" /> <asp:Button ID="btnGoUp" runat="server" CausesValidation="False" CssClass="redButtonCss" OnClick="btnGoUp_Click" Text="上传" Width="67px" style="position: relative" /> 先谢谢啦,可为什么我在输入的时候说找不到“file1”? using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //type == "jpg" || type == "png" || type == "bmp" || type == "gif" || type == "jpeg" } string str = "insert into FileImage(content) values(@content)"; string Conn = @"server=.;uid=sa;pwd=sa;database=Northwind"; protected void btnFileUpload_Click(object sender, EventArgs e) { //string imgtype = .ContentType; //文件类型 string sFile = FileUpload1.PostedFile.FileName.ToLower(); string type = Path.GetExtension(sFile); string FileType = type.Substring(type.IndexOf('.') + 1); //FileName.Substring(FileName.IndexOf('.') + 1); if (FileType == "jpg") { byte[] content = FileToByte(sFile); SaveFile(content); Response.Write("<script language='javascript'>alert('上传成功');</script>"); } else { Response.Write("<script language='javascript'>alert('图片上传失败,请重试!');</script>"); } } private byte[] FileToByte(string filepath) { byte[] ib = new byte[6000]; //byte[] ib = new byte[6000]; //获取上传的图片路径 string strFilepath = filepath; FileStream fs = new FileStream(strFilepath, FileMode.Open, FileAccess.Read); //将文件的内容读进字节数组 fs.Read(ib, 0, filepath.Length); return ib; // Response.Write("<script language='javascript'>alert('图片上传失败,请重试!" + ex.Message.ToString() + "');</script>"); } private void SaveFile(byte[] content) { SqlConnection my_conn = new SqlConnection(Conn); my_conn.Open(); SqlCommand sql_cmd = new SqlCommand(str, my_conn); sql_cmd.Parameters.Add("@content",SqlDbType.Image).Value = content; sql_cmd.ExecuteNonQuery(); //sql_cmd.ExecuteReader(); } }} 添加runat="server"参考http://www.cnblogs.com/HurYun/articles/384286.htmlhttp://www.cnblogs.com/kingjiong/archive/2008/01/13/1037084.html 如何设置web.config,以便使用session进行验证? 数据库语句问题 [跪求]请各位高手指教! vs2008有没有类似dreamweaver的本地编辑然后远程调试功能 Loing控件登录后跳转的问题 |M| 第一次学习XML 请大家帮改善一下代码 和两个问题 哪位知道 有哪些 HTML在线编辑器支持VS2005? aspnet2.0使用新手的提问,如何建立网站? 时间提取问题 在线等!!!! UBB终级问题,相信所有新手朋友梦寐以求的提问方式???求教!! 登录时候的sql这样写安全不只过滤单引号 |zyciis| 200分求 如何给用户控件设置默认按钮 急 第三贴:改错 谢谢
{
//文件上传
try
{
if (Fupload.PostedFile.FileName == "")
return "";
string dir = DateTime.Now.ToString("yyyy");
if (!Directory.Exists(Server.MapPath("uploadpic")))
{
Directory.CreateDirectory(Server.MapPath("uploadpic"));
if (!Directory.Exists(Server.MapPath("uploadpic")))
return "";
}
Random rd = new System.Random();
string filename;
string extname; if (Fupload.PostedFile.FileName != "")
{
extname = Fupload.PostedFile.FileName.Substring(Fupload.PostedFile.FileName.LastIndexOf(".") + 1).ToUpper();
if ("JPG".IndexOf(extname) == -1)
{
return "";
} filename = dir + "\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + rd.Next(1000).ToString() + "." + extname;
string mfileExtension = System.IO.Path.GetExtension(filename);
Fupload.PostedFile.SaveAs(Server.MapPath("uploadpic\\") + filename);
return "uploadpic\\" + filename;
}
return ""; }
catch { return ""; }
}
<input id="File2" type="file" runat="server" /> string imgUrl = this.UploadPicFile(this.File1);
if (imgUrl == "")
{
return;
}
protected void btnGoUp_Click(object sender, EventArgs e)
{
string str = this.File1.PostedFile.FileName;
if (str == "")
{
Response.Write(bc.MassageBox("请选择上传图片!"));
return;
} string ss = str.Substring(str.LastIndexOf("\\") + 1);
string s = Server.MapPath("~\\photo\\" + ss);
path = "~\\photo\\" + ss;
if (File.Exists(s))
{
Response.Write(bc.MassageBox("该文件已经存在,请重新命名!!!"));
return;
}
this.File1.PostedFile.SaveAs(s);
Response.Write(bc.MassageBox("上传成功!"));
}
存数据库的时候就存path就是当前图片的路径了页面这样<input id="File1" runat="server" class="InputCss" style="width: 287px" type="file" />
<asp:Button ID="btnGoUp" runat="server" CausesValidation="False" CssClass="redButtonCss"
OnClick="btnGoUp_Click" Text="上传" Width="67px" style="position: relative" />
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//type == "jpg" || type == "png" || type == "bmp" || type == "gif" || type == "jpeg"
} string str = "insert into FileImage(content) values(@content)";
string Conn = @"server=.;uid=sa;pwd=sa;database=Northwind"; protected void btnFileUpload_Click(object sender, EventArgs e)
{
//string imgtype = .ContentType; //文件类型
string sFile = FileUpload1.PostedFile.FileName.ToLower();
string type = Path.GetExtension(sFile);
string FileType = type.Substring(type.IndexOf('.') + 1); //FileName.Substring(FileName.IndexOf('.') + 1); if (FileType == "jpg")
{ byte[] content = FileToByte(sFile);
SaveFile(content);
Response.Write("<script language='javascript'>alert('上传成功');</script>");
}
else
{
Response.Write("<script language='javascript'>alert('图片上传失败,请重试!');</script>");
}
}
private byte[] FileToByte(string filepath)
{
byte[] ib = new byte[6000];
//byte[] ib = new byte[6000];
//获取上传的图片路径
string strFilepath = filepath;
FileStream fs = new FileStream(strFilepath, FileMode.Open, FileAccess.Read);
//将文件的内容读进字节数组
fs.Read(ib, 0, filepath.Length);
return ib;
// Response.Write("<script language='javascript'>alert('图片上传失败,请重试!" + ex.Message.ToString() + "');</script>");
}
private void SaveFile(byte[] content)
{
SqlConnection my_conn = new SqlConnection(Conn);
my_conn.Open(); SqlCommand sql_cmd = new SqlCommand(str, my_conn); sql_cmd.Parameters.Add("@content",SqlDbType.Image).Value = content; sql_cmd.ExecuteNonQuery();
//sql_cmd.ExecuteReader();
} }
}
参考
http://www.cnblogs.com/HurYun/articles/384286.html
http://www.cnblogs.com/kingjiong/archive/2008/01/13/1037084.html