62,041
社区成员
发帖
与我相关
我的任务
分享
protected void btn_提交_Click(object sender, EventArgs e)
{
//上传图片
string uploadName = InputFile.Value;//获取待上传图片的完整路径,包括文件名
//string uploadName = InputFile.PostedFile.FileName;
string pictureName = "";//上传后的图片名,以当前时间为文件名,确保文件名没有重复
if (InputFile.Value != "")
{
int idx = uploadName.LastIndexOf(".");
string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名
pictureName = DateTime.Now.Ticks.ToString() + suffix;
}
try
{
if (uploadName != "")
{
string path = Server.MapPath("~/uploads/");
InputFile.PostedFile.SaveAs(path + pictureName);
//保存图片
conn.ConnectionString = connectionString;
conn.Open();
SqlCommand cmd = conn.CreateCommand();
FileStream fs = new FileStream(uploadName, FileMode.Open, FileAccess.Read);
int imgLen = (int)fs.Length;
Byte[] ImgBinaryData = new Byte[imgLen];
fs.Read(ImgBinaryData, 0, imgLen);
cmd.Parameters.Add("@图片", SqlDbType.Image);
cmd.Parameters["@图片"].Value = ImgBinaryData;
cmd.Parameters.AddWithValue("@内容", txt_内容.Text);
cmd.Parameters.AddWithValue("@时间", datatime.Text);
cmd.CommandText = "insert into 商城托管_成功案例(detail,image,datetime) values(@内容,@图片,@时间)";
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("<script>alert('保存成功!')</script>");
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
#region 上传文件到数据库和服务器
public void FN_UpFiles()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
try
{
for( int iFile = 0 ; iFile < files.Count ; iFile++ )
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName = "";//定义文件名
//string fileExtension = "";
fileName = Path.GetFileName(postedFile.FileName);//得到上传文件的完整名称 即文件名+后缀名
int index = fileName.IndexOf(".");
string FileType = fileName.Substring(index).ToLower();//截取文件后缀名
//FileTypeImg = "../FileTypeimg/" + hz + ".gif";
Guid fileGuid = Guid.NewGuid();//生成新的文件名称 以GUID命名防止文件名相同
string NewFileName = fileGuid.ToString();//新的文件名
NewFileName = NewFileName + FileType;//新的文件名+后缀名
if( postedFile.ContentLength > 2097151 * 1024 )//判断是否大于配置文件中的上传文件大小
{
Page.RegisterStartupScript("提示" ,"<script language='javascript'>alert('对不起您的上传资源过大!');return;</script>");
return;
}
else
{
if( fileName != "" )//如果文件名不为空
{
try
{
//文件虚拟路径
string strpath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName;
try
{
NRModel.File model = new NRModel.File();
NRBLL.File bf = new NRBLL.File();
Guid guid1 = Guid.NewGuid();
Guid guid2 = new Guid(FolderId);
Guid guid3 = Guid.NewGuid();
Guid guid4 = Guid.NewGuid();
model.Fileid = guid1;
model.Folderid = guid2;
model.Filepath = strpath;
model.FileNam = fileName;
model.FileSize = postedFile.ContentLength;
model.Decription = TextArea1.InnerText.ToString();
model.CreateOn = DateTime.Now;
model.CreateBy = guid3;
model.ModefyBy = guid4;
if( bf.FN_AddNewRes(model) > 0 )
{
NR.Error.Log.LogType("上传资源" + fileName + "成功!" + "服务器路径:" + strpath);
//保存文件到指定目录(虚拟目录)
postedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName);
Page.RegisterStartupScript("提示" ,"<script language='javascript'>alert('上传成功!');self.opener.location.reload();window.close();</script>");
}
} catch( Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
} catch( Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
}
else
{
Response.Write("上传文件不能为空!");
NR.Error.Log.LogType("文件不能为空!");
}
}
}
} catch( System.Exception ex )
{
NR.Error.Log.LogType(ex.ToString());
}
}
#endregion