【问题标题】:multiple file upload and converting it into html format多个文件上传并转换成html格式
【发布时间】:2013-04-17 03:16:12
【问题描述】:

我正在尝试上传多个文件,并通过将它们放入 for 循环同时将它们转换为 .html 格式。多个文件上传完成并存储到“上传”文件中,但只有第一个文件被转换为.htm 格式,而不是全部。

这是我的代码:

protected void btnUpload_Click(object sender, EventArgs e)
    {

        HttpFileCollection fileCollection = Request.Files;
        //Code to check if user has selected any file on the form
        if (!(fUpload1.HasFile))
        {
            lblMessage1.Text = "Please choose file to upload";
        }
        else
        {
            for (int i = 0; i < fileCollection.Count; i++)
            {
                try
                {
                    HttpPostedFile uploadfile = fileCollection[i];
                    string fileName = System.IO.Path.GetFileName(uploadfile.FileName);

                    //To check the file extension if it is word document or something else
                    //string strFileName = fUpload1.FileName;
                    string[] strSep = fileName.Split('.');
                    int arrLength = strSep.Length - 1;
                    string strExt = strSep[arrLength].ToString().ToUpper();

                    //Save the uploaded file to the folder
                    strPathToUpload = Server.MapPath("Uploaded2");

                    //Map-path to the folder where html to be saved
                    strPathToConvert = Server.MapPath("Aadi2");

                    object FileName = strPathToUpload + "\\" + fileName;
                    object FileToSave = strPathToConvert + "\\" + fileName + ".htm";

                    if (strExt.ToUpper().Equals("DOC") | strExt.ToUpper().Equals("DOCX"))
                    {
                        uploadfile.SaveAs(strPathToUpload + "\\" + fileName);
                        lblMessage1.Text = "File uploaded successfully";
                        //open the file internally in word. In the method all the parameters should be passed by object reference
                        objWord.Documents.Open(ref FileName, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing, ref missing);
                        //Do the background activity
                        objWord.Visible = false;


                        Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;
                        oDoc.SaveAs(ref FileToSave, ref fltDocFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                        lblMessage1.Text = fileName + " done";

                    }
                    else if (strExt.ToUpper().Equals("JPG"))
                    {
                        strPathToUpload = Server.MapPath("images");
                        uploadfile.SaveAs(strPathToUpload + "\\" + fUpload1.FileName);
                        lblMessage1.Text = "logo uploaded successfully";

                    }
                    else if (strExt.ToUpper().Equals("TXT"))
                    {
                        strPathToUpload = Server.MapPath("name");
                        fUpload1.SaveAs(strPathToUpload + "\\" + fUpload1.FileName);
                        lblMessage1.Text = "Website name uploaded successfully";

                    }
                    else if (strExt.ToUpper().Equals("MP4"))
                    {
                        strPathToUpload = Server.MapPath("video");
                        fUpload1.SaveAs(strPathToUpload + "\\" + fUpload1.FileName);
                        lblMessage1.Text = "Video uploaded successfully";

                    }

                    else
                    {
                        lblMessage1.Text = "Invalid file selected!";
                    }
                    //Close/quit word
                    objWord.Quit(ref missing, ref missing, ref missing);
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
    } 

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    您不能使用asp.net中的一个文件上传控件上传多个文件。

    在此处查看解决方案:asp.net multiple uploads with multiple fileupload controlhttp://www.dotnetcurry.com/ShowArticle.aspx?ID=68

    或者要上传多个文件,您可以在客户端生成多个上传控件或创建用户定义的控件。

    【讨论】:

    • 我是 c# 的初学者...所以如果您能指出我在代码中犯了什么错误..
    • 上传成功并保存在“uploaded”文件夹中..但只有第一个doc文件被转换为.htm格式
    【解决方案2】:

    您正在使用 Interop 转换 DOC 文件 - 这是according to Microsoft NOT SUPPORTED in ASP.NET etc.

    您可以使用其他库,例如 OpenXML from Microsoft(免费)或 Aspose.Words(商业),它们不依赖于 Office,并且在像您这样的服务器方案中得到完全支持。

    【讨论】:

    • 谢谢你..我会调查一下
    【解决方案3】:

    如果上传的文件存在于目录中,则不要使用 httpfilecollection,而是使用 Directory.GetFiles。它将在您的情况下从目录中返回特定文件 *.doc 文件,然后将其转换为 .html。

    string[] filePaths = Directory.GetFiles(@"d:\files\", "*.doc");
    

    【讨论】:

      猜你喜欢
      • 2021-03-07
      • 2017-03-08
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 2021-03-14
      • 1970-01-01
      相关资源
      最近更新 更多