展开全部
#region 上传文件的方法
/// <summary>
/// 上传文件方法
/// </summary>
/// <param name="myFileUpload">上传控件ID</param>
/// <param name="allowExtensions">允许上传的扩展文件名类型,如:string[] allowExtensions = { ".doc", ".xls", ".ppt", ".jpg", ".gif" };</param>
/// <param name="maxLength">允许上传的最大大小,以M为单位</param>
/// <param name="savePath">保存文件的目录,注意是绝对路径,如:Server.MapPath("~/upload/");</param>
/// <param name="saveName">保存的文件名,如果是""则以原文件名保存</param>
private void Upload(FileUpload myFileUpload, string[] allowExtensions, int maxLength, string savePath, string saveName)
{
// 文件格式是否允许上传
bool fileAllow = false;
//检查是否有文件案
if (myFileUpload.HasFile)
{
// 检查文件大小, ContentLength获取的是字节,转成M的时候要除以2次1024
if (myFileUpload.PostedFile.ContentLength / 1024 / 1024 >= maxLength)
{
throw new Exception("只能上传小于2M的文件!");
}
//取得上传文件之扩展文件名,并转换成小写字母
string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
string tmp = ""; // 存储允许上传的文件后缀名
//检查扩展文件名是否符合限定类型
for (int i = 0; i < allowExtensions.Length; i++)
{
tmp += i == allowExtensions.Length - 1 ? allowExtensions[i] : allowExtensions[i] + ",";
if (fileExtension == allowExtensions[i])
{
fileAllow = true;
}
}
if (fileAllow)
{
try
{
string path = savePath + (saveName == "" ? myFileUpload.FileName : saveName);
//存储文件到文件夹
myFileUpload.SaveAs(path);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
else
{
throw new Exception("文件格式不符,可以上传的文件格式为:" + tmp);
}
}
else
{
throw new Exception("请选择要上传的文件!");
}
}
#endregion
以下是测试的代码:
try
{
string[] ss = { ".jpg", ".gif" };
string path = Request.MapPath("~/upload/");
Upload(FileUpload1, ss, 1, path, "");
Label1.Text = "文件上传成功!";
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
/// <summary>
/// 上传文件方法
/// </summary>
/// <param name="myFileUpload">上传控件ID</param>
/// <param name="allowExtensions">允许上传的扩展文件名类型,如:string[] allowExtensions = { ".doc", ".xls", ".ppt", ".jpg", ".gif" };</param>
/// <param name="maxLength">允许上传的最大大小,以M为单位</param>
/// <param name="savePath">保存文件的目录,注意是绝对路径,如:Server.MapPath("~/upload/");</param>
/// <param name="saveName">保存的文件名,如果是""则以原文件名保存</param>
private void Upload(FileUpload myFileUpload, string[] allowExtensions, int maxLength, string savePath, string saveName)
{
// 文件格式是否允许上传
bool fileAllow = false;
//检查是否有文件案
if (myFileUpload.HasFile)
{
// 检查文件大小, ContentLength获取的是字节,转成M的时候要除以2次1024
if (myFileUpload.PostedFile.ContentLength / 1024 / 1024 >= maxLength)
{
throw new Exception("只能上传小于2M的文件!");
}
//取得上传文件之扩展文件名,并转换成小写字母
string fileExtension = System.IO.Path.GetExtension(myFileUpload.FileName).ToLower();
string tmp = ""; // 存储允许上传的文件后缀名
//检查扩展文件名是否符合限定类型
for (int i = 0; i < allowExtensions.Length; i++)
{
tmp += i == allowExtensions.Length - 1 ? allowExtensions[i] : allowExtensions[i] + ",";
if (fileExtension == allowExtensions[i])
{
fileAllow = true;
}
}
if (fileAllow)
{
try
{
string path = savePath + (saveName == "" ? myFileUpload.FileName : saveName);
//存储文件到文件夹
myFileUpload.SaveAs(path);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
else
{
throw new Exception("文件格式不符,可以上传的文件格式为:" + tmp);
}
}
else
{
throw new Exception("请选择要上传的文件!");
}
}
#endregion
以下是测试的代码:
try
{
string[] ss = { ".jpg", ".gif" };
string path = Request.MapPath("~/upload/");
Upload(FileUpload1, ss, 1, path, "");
Label1.Text = "文件上传成功!";
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
展开全部
//写入数据库
OpenFileDialog sf = new OpenFileDialog();
sf.Filter = "(*.jpg)|*.jpg";
sf.ShowDialog();
Image img = Image.FromFile(sf.FileName);
this.pictureBox1.Image = img;
SqlConnection conn = new SqlConnection("server=.\\sqlexpress;uid=sa;database=master");
SqlCommand cmd = new SqlCommand("insert into imgs values(@img)", conn);
cmd.Parameters.Add(new SqlParameter("@img", SqlDbType.Image));
MemoryStream ms = new MemoryStream();
img.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
cmd.Parameters[0].Value = ms.ToArray();
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
ms.Close();
//读数据库
SqlConnection conn = new SqlConnection("server=.\\sqlexpress;uid=sa;database=master");
conn.Open();
SqlCommand cmd = new SqlCommand("select img from imgs where id = 1",conn);
byte[]a = (byte[])cmd.ExecuteScalar();
MemoryStream ms = new MemoryStream();
ms.Write(a, 0, a.Length);
this.pictureBox1.Image = new Bitmap(ms);
OpenFileDialog sf = new OpenFileDialog();
sf.Filter = "(*.jpg)|*.jpg";
sf.ShowDialog();
Image img = Image.FromFile(sf.FileName);
this.pictureBox1.Image = img;
SqlConnection conn = new SqlConnection("server=.\\sqlexpress;uid=sa;database=master");
SqlCommand cmd = new SqlCommand("insert into imgs values(@img)", conn);
cmd.Parameters.Add(new SqlParameter("@img", SqlDbType.Image));
MemoryStream ms = new MemoryStream();
img.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
cmd.Parameters[0].Value = ms.ToArray();
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
ms.Close();
//读数据库
SqlConnection conn = new SqlConnection("server=.\\sqlexpress;uid=sa;database=master");
conn.Open();
SqlCommand cmd = new SqlCommand("select img from imgs where id = 1",conn);
byte[]a = (byte[])cmd.ExecuteScalar();
MemoryStream ms = new MemoryStream();
ms.Write(a, 0, a.Length);
this.pictureBox1.Image = new Bitmap(ms);
参考资料: http://hi.baidu.com/davin_lan/blog/item/35d35b89f3eaf99da5c27212.html
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询