当前位置:编程学习 > C#/ASP.NET >>

菜鸟求助:.docx文件不能存进数据库,而.doc和.txt文件则成功存取

docx文件以二进制文件流的方式保存不进数据库,而doc和txt文件则可以保存进数据库。求帮助,代码哪里写错了?写的代码如下:
public partial class KnowledgeNet_KMAdmin_FileAdd1 : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
         if(UploadFile.HasFile)
{
                //可加上传文件类型、大小限定代码
                //指定文件在服务器的保存路径
                string savePath = Server.MapPath("~/upload/");
                if (!System.IO.Directory.Exists(savePath))
                {
                    //需要注意的是,需要对这个物理路径有足够的权限,否则会报错 
                    //另外,这个路径应该是在网站之下,而将网站部署在C盘却把上传文件保存在D盘 
                    System.IO.Directory.CreateDirectory(savePath);
                }
                savePath = savePath + "\\" + UploadFile.FileName;
                UploadFile.SaveAs(savePath);//保存文件 
                //不过需要注意的是,在客户端访问却需要指定的是URL地址,而不是在服务器上的物理地址 
               // literal.Text = string.Format("<a href='upload/{0}'>upload/{0}</a>",UploadFile.FileName); 
                string KenAssetID;

                if (Session["KenAssetID"] != null)
                {
                    KenAssetID = Session["KenAssetID"].ToString();
                    constring ha = new constring();
                    SqlConnection con = new SqlConnection(ha.constr);
                    con.Open();
                    //得到提交的文件
                    Stream fileDataStream = UploadFile.PostedFile.InputStream;
                    //创建数组
                    byte[] myData = new byte[UploadFile.PostedFile.ContentLength+1];
                    //把文件流填充到数组
                    fileDataStream.Read(myData, 0, UploadFile.PostedFile.ContentLength);
                    // 关闭数据流  
                    fileDataStream.Close();
                    //得到文件名称
                    string FileName = UploadFile.PostedFile.FileName;
                    string url = FileName;
                    FileName = Path.GetFileName(savePath);
                    /*储存文件*/
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("Select * from Kn_Asset where KenAssetID='" + KenAssetID + "'", con);
                    SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
                    DataSet dataSet = new DataSet("asset");
                    sqlDataAdapter.Fill(dataSet, "asset");

                    /*System.Data.DataRow dataRow = dataSet.Tables["asset"].NewRow();*/
                    dataSet.Tables["asset"].Rows[0]["CTime"] = DateTime.Now.ToString();
                    dataSet.Tables["asset"].Rows[0]["FileName"] = FileName;
                    dataSet.Tables["asset"].Rows[0]["FileType"] = UploadFile.PostedFile.ContentType;
                    dataSet.Tables["asset"].Rows[0]["FileStore"] = myData;
                    /*dataRow["FileLength"]=UploadFile.PostedFile.ContentLength;*/
                    /*dataSet.Tables["asset"].Rows.Add(dataRow);*/

                    sqlDataAdapter.Update(dataSet, "asset");



                    /*模拟自动分类方法*/
                    //textminer
                    // KenAssetID,
                    int kid = int.Parse(KenAssetID);
                    TextMinerP miner = new TextMinerP();
                    miner.textminer(url, kid, int.Parse(Session["MmID"].ToString()));
                    /******************/


                    Response.Write("<script>alert('上传成功!')</script>");
                    /*Response.Write("<script language=javascript>windows.close();</script>");*/
                }
                }

    }
}

--------------------编程问答--------------------
看不懂 你可以单补调试么 --------------------编程问答-------------------- 这种问题完全就是人品问题.存到数据时docx跟doc完全就是一样的.
检查一下你的程序,是不是限制了docx的上传.或是你的文件大太了
--------------------编程问答-------------------- 真特么屌,Word内容存数据库啊 --------------------编程问答--------------------
引用 3 楼 xupeihuagudulei 的回复:
真特么屌,Word内容存数据库啊

谢谢LS,那个不是文件太大的问题,4M的doc能上传,任何的docx都不能够上传。就是docx文件的内容存不进去,doc和txt没有任何问题,极度纳闷…… --------------------编程问答-------------------- 还真不清楚,按理来说即使服务器上不能打开docx文件应该也能正常存储才对呀 --------------------编程问答-------------------- 到哪一步失败了,错误提示是什么? --------------------编程问答--------------------
引用 6 楼 yuwenge 的回复:
到哪一步失败了,错误提示是什么?

上docx就出现这个异常,查数据库无文件、无存储。doc和txt就完全可以。

将截断字符串或二进制数据。
语句已终止。 
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Data.SqlClient.SqlException: 将截断字符串或二进制数据。
语句已终止。

源错误: 





行 1070:                    /*dataSet.Tables["asset"].Rows.Add(dataRow);*/
行 1071:                    
行 1072:                    sqlDataAdapter.Update(dataSet, "train");
行 1073:                    /******************/
行 1074:                    GridView3.EditIndex = -1; 

源文件: c:\inetpub\wwwroot\知识网络2013\源程序0\KnowledgeNet\KMap\netsystem.aspx.cs    行: 1072 

堆栈跟踪: 





[SqlException (0x80131904): 将截断字符串或二进制数据。
语句已终止。]
   System.Data.Common.DbDataAdapter.UpdatedRowStatusErrors(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) +1460411
   System.Data.Common.DbDataAdapter.UpdatedRowStatus(RowUpdatedEventArgs rowUpdatedEvent, BatchCommandInfo[] batchCommands, Int32 commandCount) +45
   System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping) +2475
   System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping) +41
   System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) +170
   Migrated_NetSystem.GridView3_RowUpdating(Object sender, GridViewUpdateEventArgs e) in c:\inetpub\wwwroot\知识网络2013\源程序0\KnowledgeNet\KMap\netsystem.aspx.cs:1072
   System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +122
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +792
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +738
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +88
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +156
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9642610
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
 
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,