【问题标题】:Upload form not working - File is being used by another process上传表单不起作用 - 文件正被另一个进程使用
【发布时间】:2014-01-10 13:32:59
【问题描述】:

我有一个 C# 文件上传,旨在从 DocX 文档中提取 XML 标记,我面临的问题是,当文件上传时,出现“文件正在被另一个进程使用”错误向上。尝试删除文档表明它正在被 IIS 进程管理器使用。

有没有办法停止我的代码以使其继续运行?

<script runat="server">


 //foreach (DataRow row in table.Rows)
 //{
  //  string dbColumnNames = (selectedData.ToString());
     //send files
 //}

    public string _TempFileLocation = ""; //Used to locate Word Document File Path 


        //THE USER UPLOAD CONTROL. users use this to upload the document to the server
        public void XMLextractor(string _filePath)
        {
            //XML extraction code
           displayFilepath.Text = _filePath;
           _TempFileLocation = _filePath;

        }

        //names the script manager which will be used when the user attempts to upload a form / gives an error if they incorrectly attempt to upload
        protected void UploadButton_Click(object sender, EventArgs e)
        {
            //if file is located
            if (FileUploadControl.HasFile)
            {
                try
                {
                    //allow content type of document / docx
                    if (FileUploadControl.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
                    {

                        if (FileUploadControl.PostedFile.ContentLength < 10485760) // 10mb)
                        {
                            //name the filename, find the path of the name
                            string filename = Path.GetFileName(FileUploadControl.FileName);
                            //path of server upload (we just need to save it as a variable to be found on the next page, as it will be made / deleted
                            FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                            //update the label with file uploaded
                            StatusLabel.Text = "Upload status: File uploaded!";

                            XMLextractor(Server.MapPath("~/") + filename);

                            //move onto template wizard page
                            //Response.Redirect("http://portal.acoura.com/admin/templatewizard.aspx", false);

                            WordprocessingDocument _TempDoc = WordprocessingDocument.Open(Server.MapPath("~/") + filename, true);

                            XDocument xdoc = XDocument.Load(Server.MapPath("~/") + filename);
                            //query to find particular descendants
                            var lv1s = from document in xdoc.Descendants("table")
                                       select new
                                       {
                                           Header = document.Attribute("name").Value,
                                           Children = document.Descendants("tag")
                                       };

                            //Loop through results
                            StringBuilder result = new StringBuilder();
                            foreach (var lv1 in lv1s)
                                        {
                                            result.AppendLine(lv1.Header);
                                            foreach (var lv2 in lv1.Children)
                                            result.AppendLine("     " + lv2.Attribute("name").Value);
                                        }

                            //the label should contain the content controls of the document, using the class, XMLfromDocument                                                                                                                                           
                            labelContentControls.Text = fileUpload_Displayx(XMLfromDocument.GetContentControls(_TempDoc));
                        }

                        else
                            //display the size the file needs to be less than
                            StatusLabel.Text = "Upload status: The file has to be less than 10mb!";
                    }
                    else
                        //tell the user only docx files are accepted
                        StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
                }
                catch (Exception ex)
                {
                    //display the exception message, in which case it would be either size / type / if it's present
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }


            }

        }

        //needs to be replaced with the variable found in descendants / var tagContent
        public string fileUpload_Displayx(XElement _contentcontrol)
        {
            string str = "";
            str = _contentcontrol.Name.ToString();

            return str;
        }


   //public static displayDatabase(object sender, EventArgs e)
  // {

   //}
   //run the validate button on templatewizard, will mark up any problems or give green light


   //if red, allow users to replace fields in the left column, from ones in the atabase on the right


   //display upload button when validation is succesful. When Upload button runs, Take to new 
   // / existing page of reports, allow users to download this 






</script>

【问题讨论】:

    标签: c# .net xml file-upload linq-to-xml


    【解决方案1】:

    您正在打开文件而不在此行关闭它:

    WordprocessingDocument _TempDoc = WordprocessingDocument.Open(Server.MapPath("~/") + filename, true);
    

    然后你用 xDocument.Load() 再次打开它:

    XDocument xdoc = XDocument.Load(Server.MapPath("~/") + filename);
    

    我认为这是错误发生的地方。

    如果你先处理XDocument 需要做的所有事情,然后打开和关闭WordProcessingDocument.Open() 行以获取内容控件,你应该没问题。

    基本上一次只有一个进程可以打开和读取或修改一个文件,因此如果需要执行来自两个不同来源的两个操作,则必须对文件依次执行。

    您还可以通过 FileStream 打开文件,然后将内容加载到内存和您的XDocument,因此无需让XDocument 和WordProcessingDocument 同时打开文件两次。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2011-02-24
      • 2011-04-13
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多