送分了!上传文件到数据库,存入文件路径名,并显示出来(.net) 用路径名的,不是用流的
解决方案 »
- copy 1W+文件量怎么优化?
- 不同命名空间调用配置文件的问题
- 关于Activator.CreateInstance和Assembly.Load(_PATH).CreateInstance的问题
- 来个算法题活动下大家的大脑!
- 多编程语言协同开发的问题?!希望高手指点
- winform中树与数据库绑定后上下移动树节点的问题
- 请问.Net网络编程与以往win32有何不同?
- 菜鸟求助,如何写入保存的excel或者txt到datagridview里?
- 打印机滚动换纸的问题,急等(高手进)
- 怪事情,请帮忙!.....
- 基础问题 -- 怎么把一个普通数组转换成ArrayList?
- c#调用VC++6.0的DLL出错
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;
using System.IO;public partial class Default2 : System.Web.UI.Page
{
// protected string DefaultTitle = System.Configuration.ConfigurationManager.AppSettings["SystemName"].ToString(); //获取系统名称
protected string strConn = System.Configuration.ConfigurationManager.AppSettings["strSqlConn"].ToString(); //获取数据库连接字条串
protected string UploadFileTypes = System.Configuration.ConfigurationManager.AppSettings["UploadFileType"].ToString(); //获取允许上传的格式
protected string UploadSavePath = System.Configuration.ConfigurationManager.AppSettings["UploadSavePath"].ToString(); //获取附件保存根目录,如upfiles/ protected void Page_Load(object sender, EventArgs e)
{ }
protected void BtUpLoad(object sender, EventArgs e)
{
string filePath; //文件路径
string fileName; //附件名称
int fileSize; //附件大小
string fileType; //附件格式
string sAbsoluteSavePath; //服务器端附件保存根目录
string sFileSaveName; //附件保存在服务器上的名称
int tt;
string savePath;
if (fileuploadid.PostedFile.FileName != "")
{
savePath = DateTime.Now.ToString("yyyyMM")+"/"; //指定上传附件保存的目录,根据年月生成 如200804/
Exception excp = InitUploadSaveDirectory(savePath);
//如果返回异常则退出程序
if (excp != null)
{
Response.Write(excp.ToString());
return;
}
filePath = fileuploadid.PostedFile.FileName;
fileName = Path.GetFileName(filePath); //原始附件名称
fileSize = fileuploadid.PostedFile.ContentLength; //附件大小
tt = fileName.LastIndexOf(".");
fileType = fileName.Substring(tt).ToLower(); //附件格式,小写
//获取服务端附件保存根目录,如D:\上传\upfiles\200804/
sAbsoluteSavePath = Path.Combine(Request.MapPath(UploadSavePath), savePath);
//指定附件保存在服务器上的名称(根本上传时间生成,重名处理)
sFileSaveName = DateTime.Now.ToString("MMdd") + DateTime.Now.ToString("hhmmss") + fileType; if (fileSize > 0 && fileType.Length > 0 && ("|" + UploadFileTypes + "|").IndexOf("|" + fileType + "|") > -1)
{
try
{
string sqlInsert;
fileuploadid.PostedFile.SaveAs(Path.Combine(sAbsoluteSavePath, sFileSaveName));
//SqlConnection sqlcon = new SqlConnection(strConn);
//sqlInsert = "insert into web_AttachmentInfo(FileName,OrigialName,SavePath,FileSize,Uptime) values(''" + sFileSaveName + "'',''" + fileName + "'',''" + sAbsoluteSavePath + "'',''" + fileSize + "'',''" + DateTime.Now.ToString("yyyy - MM - dd") + "'')";
//SqlCommand sqlcom = new SqlCommand(sqlInsert,sqlcon);
//sqlcon.Open();
//sqlcom.ExecuteNonQuery();
Response.Write("<script>alert(''上传成功!'');</script>"); }
catch (Exception ex)
{
Response.Write("错误:" +ex.Message);
}
}
}
else
{
Response.Write("<script>alert(''靠,先选个附件啊!'');</script>");
}
}
/// <summary>
/// 初始化上传目录
/// </summary>
/// <param name="savePath">附件保存目录的格式或者路径</param>
/// <returns>如果创建保存路径失败将返回异常</returns>
private Exception InitUploadSaveDirectory(string savePath)
{
if (UploadSavePath == null)
{
throw new ArgumentNullException("UploadSavePath");
}
//获取服务端附件保存根目录,如D:\上传\upfiles\200804/
string sAbspath = Request.MapPath(UploadSavePath);
//如果路径不存在就试图创建根目录
if (!Directory.Exists(sAbspath) )
{
Directory.CreateDirectory(sAbspath);
}
sAbspath = Path.Combine(sAbspath,savePath);
//如果保存附件目录不存在就创建该保存目录
if(!Directory .Exists(sAbspath))
{
Directory.CreateDirectory(sAbspath);
}
return null;
}
}
<htmL>
<head id="Head1" runat="server">
<title>
</title>
</head>
<body>
<form id="form1" runat="server">
<br />
<br />
<br />
<table align="center" border="1" cellpadding="0" cellspacing="0" bordercolor="#3399ff" style="width: 749px">
<tr>
<td align="center" style="width:267px" >
<input type="file" runat="server" id="fileuploadid" style="width: 439px" />
</td>
<td align="right" style="width:150px"><asp:Button runat="server" ID="btupload" OnClick="BtUpLoad" Text="上传" />
</td>
</tr>
</table>
</form>
</body>
</html>
<form id="form1" runat="server">
<div class="form3d1" id="frmdatabox">
<div style="left:40px;top:60px;position:absolute;" nowrap="true">
浏览:<asp:FileUpload ID="inFile" runat="server" size="35" />
</div>
</div>
<asp:Image Width="200" Height="200" ID="imgCagegoryImage" runat="server" />
<div style="left:200px;top:120px;position:absolute;" nowrap="true">
<asp:Button ID="btUpLoad" runat="server" OnClick="btUpLoad_Click"/>
</div>
</form>.cs
protected void btUpLoad_Click(object sender, EventArgs e)
{
try
{
if (this.inFile.FileBytes.Length == 0)
{
Alert("文件为空");
} bool fileOK = false; if (this.inFile.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(inFile.FileName).ToLower(); string[] allowedExtensions = { ".gif", ".jpg", ".png", ".bmp", }; for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
if (fileOK)
{
try
{
string fileName = System.DateTime.Now.ToString("yyyyMMddhhmmss") + this.inFile.FileName;
this.inFile.SaveAs(Server.MapPath("~/upload/"+ fileName));
}
catch
{
}
finally
{
}
} else
{
Alert("UploadFailure");
}
}
catch
{
Alert("UploadFailure");
}
}
string FilePath = Server.MapPath("Word文档的地址");
Response.WriteFile(FilePath);
Response.End();
要求客户端安装word
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment;filename="+fileName+".doc"); Response.AddHeader("Content-type", "application");
Response.ContentType = "application/ms-html";
Response.Write(strWord);
Response.Flush();
Response.Close();
把上面的输出的代码写在内嵌的frame的src对应的aspx页面里面.呵呵
Response.Clear();
Response.AppendHeader("Content-type", "application/vnd.ms-word");
Response.AppendHeader("Content-Disposition", "filename=Communications.doc");
Response.WriteFile(@"D:\super_down\ibs_root\20081103034747.doc");
这样试下,如果不行的话我也没办法了.除非真的在客户端设置,毕竟怎么样打开,应该是客户端的事