送分了!上传文件到数据库,存入文件路径名,并显示出来(.net) 用路径名的,不是用流的

解决方案 »

  1.   

    using System;
    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>
      

  2.   

    网上一搜一大堆aspx
       <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");
                }
            }
      

  3.   

     网上多用二进制的,我就想存入文件路径名,存到数据库表的一个字段中,如何让word文件窗口中显示
      

  4.   

      比如说,变量aa存着word文件路径,如何在IE显示word文档
      

  5.   

    Response.Contentype   =   "Appliction/msword";   
      string   FilePath   =   Server.MapPath("Word文档的地址");   
      Response.WriteFile(FilePath);   
      Response.End();
    要求客户端安装word
      

  6.   

    Response.ContentEncoding = System.Text.Encoding.UTF7;
    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();    
      

  7.   

       为什么打开的不是word文件,而是.aspx代码,在dw中打开的
      

  8.   

        好的,能不能让它打开在指定的窗口,比如内嵌iframe中
      

  9.   


    把上面的输出的代码写在内嵌的frame的src对应的aspx页面里面.呵呵
      

  10.   

        对这样也行,我原先想用控件的,果然高手啊!打开word文件时,可不可以不出现word文件打开,下载的对话框,是不是只能从浏览器设置,而无法从技术上实现?
      

  11.   


    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"); 
    这样试下,如果不行的话我也没办法了.除非真的在客户端设置,毕竟怎么样打开,应该是客户端的事