在Asp.net中如何把上传文件的储存地址存到数据库中,求源代码

 我来答
红星路柯基犬Dc7ad
2011-11-28 · TA获得超过134个赞
知道小有建树答主
回答量:139
采纳率:0%
帮助的人:156万
展开全部
这是我以前项目中一个上传文件的例子,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");

}
}
}
}
匿名用户
2011-11-28
展开全部
Server.MapPath(".")+this.FileUpload1.FileName.ToString()
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式