1.ASP.NET网页如何实现文件的上传(文件如:word、txt) 求控件、求
fileupload控件.数据库中只保存文件名称就可以. 在读取的时候在指定路径, 然后显示图片
bool files = false;
if (this.FileUpload1.HasFile)
{
string filesuffix = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower();
string[] suffix = { ".jpg", ".bmp", ".gif", ".pnd" };
for (int i = 0; i < suffix.Length; i++)//判断格式
{
if (filesuffix == suffix[i])
{
files = true;
}
}
if (files == true)
{
this.titleimage.ImageUrl = "~/img/" + this.FileUpload1.FileName;
this.FileUpload1.SaveAs(Server.MapPath("~/img/") + this.FileUpload1.FileName);//保存文件
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script>alert('文件上传失败!')</script>");
}
2.asp.net导出把网页导入word 的第三方控件
public void ToWord(System.Web.UI.Control ctl) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=DocLibrary.doc"); HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //更改ContentType的值为ms-word即可实现导出到Word HttpContext.Current.Response.ContentType = "application/ms-word";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); } ToWord(this.divTest); //注:divTest是控件的id 这个方法也不错 protected void btnPdf_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.Charset = "utf-8"; //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开 //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm Response.AppendHeader("Content-Disposition", "attachment;filename=table.doc"); Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); //Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档 Response.ContentType = "application/ms-word"; this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.detailContent.RenderControl(oHtmlTextWriter); //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件 Response.Write(oStringWriter.ToString()); Response.End(); }。
转载请注明出处51数据库 » asp.net上传word控件
基层工作者6