求助:
上传文件时候出现找不到文件异常,上传代码如下:
protected void GridView3_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            FileUpload fud = (FileUpload)GridView3.Rows[e.RowIndex].FindControl("FileUpload");
            Session["FileID"] = GridView3.DataKeys[e.RowIndex].Values[0].ToString();
            if (fud.HasFile)
            {                string FileID = Session["FileID"].ToString();
                constring ha = new constring();
                SqlConnection con = new SqlConnection(ha.constr);
                con.Open();
                //得到提交的文件
                Stream fileDataStream = fud.PostedFile.InputStream;
                //创建数组
                byte[] myData = new byte[fud.PostedFile.ContentLength];
                //把文件流填充到数组
                fileDataStream.Read(myData, 0, fud.PostedFile.ContentLength);
                // 关闭数据流  
                //fileDataStream.Close();
                //得到文件名称
                string FileName = fud.FileName;
               // string FileName = fud.PostedFile.FileName;
                //得到文件类型
                string FileType = fud.PostedFile.ContentType;
                //根据filename得出文本内容change
                filesore2change cg = new filesore2change();
                string change = cg.tochange(FileName);
                FileName = Path.GetFileName(FileName);
                //储存文件
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("Select * from Kn_Train where FileID='" + FileID + "'", con);
                SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
                DataSet dataSet = new DataSet("train");
                sqlDataAdapter.Fill(dataSet, "train");                //System.Data.DataRow dataRow = dataSet.Tables["asset"].NewRow();
                dataSet.Tables["train"].Rows[0]["FileName"] = FileName;
                dataSet.Tables["train"].Rows[0]["FileType"] = fud.PostedFile.ContentType;
                dataSet.Tables["train"].Rows[0]["FileSave"] = myData;
                dataSet.Tables["train"].Rows[0]["Change"] = change;
                //dataRow["FileLength"]=UploadFile.PostedFile.ContentLength;
                //dataSet.Tables["asset"].Rows.Add(dataRow);
                sqlDataAdapter.Update(dataSet, "train");
                
                GridView3.EditIndex = -1;
                AddKenTrain(TextBox1.Text);
}

解决方案 »

  1.   

    fud.SaveAs("xxxx");//xxxx为上传的路径
      

  2.   

    刚学C#不久,很多不懂。然后,刚刚查了好久不知道怎么弄。就是运行时候这段代码出现异常 //获取word文件的文本内容
        public static string DocToText(string docFileName)
        {
            //实例化COM        
            Microsoft.Office.Interop.Word.ApplicationClass Word_App = new Microsoft.Office.Interop.Word.ApplicationClass();
            object fileobj = docFileName;
            object nullobj = System.Reflection.Missing.Value;
            //打开指定文件(不同版本的COM参数个数有差异,
            //一般而言除第一个外都用nullobj就行了)
            Microsoft.Office.Interop.Word.Document wd = Word_App.Documents.Open(ref fileobj, ref nullobj, ref nullobj, ref nullobj,
                                                    ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                                                    ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                                                    ref nullobj, ref nullobj);

            //取得doc文件中的文本内容
            string outText = wd.Content.Text;
            //关闭文件
            wd.Close(ref nullobj, ref nullobj, ref nullobj);
            //关闭COM
            Word_App.Quit(ref nullobj, ref nullobj, ref nullobj);
            //返回文本内容
            return outText;
        }
    提示找不到该文件。(C:\Windows\system32\*)
      

  3.   

    我在if{}加上这段代码设置存储路径string savePath = Server.MapPath("~/upload/");
                    if (!System.IO.Directory.Exists(savePath))
                    { 
                        System.IO.Directory.CreateDirectory(savePath);
                    }
                    savePath = savePath + "\\" + fud.FileName;
                    fud.SaveAs(savePath);
    Session["FileID"]加上非空约束,这还是出现相同的问题。