在Asp.net中如何把上传文件的储存地址存到数据库中,求源代码
展开全部
这是我以前项目中一个上传文件的例子,LZ可以拿来参考:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using YTLib.Basic;
using YTLib.YTDBC;
using System.IO;
namespace siteadmin.admin
{
public partial class uploadfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["oop"] == "up")
{
string info = "";
string strSQL = "";
YTNDBObject dbo = new YTNDBObject();
dbo.DataName = "Main";
string AttachedFile = "";
string sDir = "";
YTDateTime ytime = new YTDateTime();
ytime.dDateTime = DateTime.Now;
DateTime yttime=DateTime.Now;
HttpRequest hreq = HttpContext.Current.Request;
if (hreq.Files.Count > 0)
{
HttpPostedFile hpfile = hreq.Files[0];
int exindex = hpfile.FileName.LastIndexOf(".");
if (exindex >= 0)
{
string sGuid = Guid.NewGuid().ToString();
AttachedFile = sGuid + hpfile.FileName.Substring(exindex).ToLower();
sDir = ytime.sDateTime.Substring(0, 6);
if (!Directory.Exists(HttpContext.Current.Server.MapPath(Utilities.UploadedFileDir + "/" + sDir)))
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Utilities.UploadedFileDir + "/" + sDir));
hpfile.SaveAs(HttpContext.Current.Server.MapPath(Utilities.UploadedFileDir + "/" + sDir + "/" + AttachedFile));
}
}
string ftitle = Request["filetitle"];
if (ftitle == null || ftitle.Length == 0)
ftitle = "";
strSQL = "Insert into FileSource(FileTitle,FilePath,addDate)";
strSQL += " values(@FileTitle,@FilePath,@addDate)";
dbo.PrepareCommand(strSQL);
dbo.SetCmdStrValue("@FileTitle", ftitle, 255);
dbo.SetCmdStrValue("@FilePath", sDir+"/"+AttachedFile, 255);
dbo.SetCmdStrValue("@addDate", yttime.ToString());
try
{
dbo.ExecuteNoQuery();
info = "上传成功!";
}
catch (Exception ex)
{
info = "服务器出现问题,上传失败!";
}
Response.Redirect("FileManager.aspx");
}
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using YTLib.Basic;
using YTLib.YTDBC;
using System.IO;
namespace siteadmin.admin
{
public partial class uploadfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request["oop"] == "up")
{
string info = "";
string strSQL = "";
YTNDBObject dbo = new YTNDBObject();
dbo.DataName = "Main";
string AttachedFile = "";
string sDir = "";
YTDateTime ytime = new YTDateTime();
ytime.dDateTime = DateTime.Now;
DateTime yttime=DateTime.Now;
HttpRequest hreq = HttpContext.Current.Request;
if (hreq.Files.Count > 0)
{
HttpPostedFile hpfile = hreq.Files[0];
int exindex = hpfile.FileName.LastIndexOf(".");
if (exindex >= 0)
{
string sGuid = Guid.NewGuid().ToString();
AttachedFile = sGuid + hpfile.FileName.Substring(exindex).ToLower();
sDir = ytime.sDateTime.Substring(0, 6);
if (!Directory.Exists(HttpContext.Current.Server.MapPath(Utilities.UploadedFileDir + "/" + sDir)))
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Utilities.UploadedFileDir + "/" + sDir));
hpfile.SaveAs(HttpContext.Current.Server.MapPath(Utilities.UploadedFileDir + "/" + sDir + "/" + AttachedFile));
}
}
string ftitle = Request["filetitle"];
if (ftitle == null || ftitle.Length == 0)
ftitle = "";
strSQL = "Insert into FileSource(FileTitle,FilePath,addDate)";
strSQL += " values(@FileTitle,@FilePath,@addDate)";
dbo.PrepareCommand(strSQL);
dbo.SetCmdStrValue("@FileTitle", ftitle, 255);
dbo.SetCmdStrValue("@FilePath", sDir+"/"+AttachedFile, 255);
dbo.SetCmdStrValue("@addDate", yttime.ToString());
try
{
dbo.ExecuteNoQuery();
info = "上传成功!";
}
catch (Exception ex)
{
info = "服务器出现问题,上传失败!";
}
Response.Redirect("FileManager.aspx");
}
}
}
}
2011-11-28
展开全部
Server.MapPath(".")+this.FileUpload1.FileName.ToString()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询