1

I am trying to upload multiple files and simultaneously converting them into .html format by putting them into a for loop. Multiple files upload is done and stored into "uploaded" file but only the first file is converted into .htm format, not all.

Here is my code:

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);
                }
            }
        }
    } 
cheesemacfly
  • 11,622
  • 11
  • 53
  • 72

3 Answers3

0

You can not upload multiple file using one File upload control in asp.net.

Check here for solution: asp.net multiple uploads with multiple fileupload control and http://www.dotnetcurry.com/ShowArticle.aspx?ID=68

or To upload multiple files you can generate multiple upload controls on client side or create user defined control.

Community
  • 1
  • 1
Sweety
  • 159
  • 3
0

You are using Interop to convert DOC files - which is according to Microsoft NOT SUPPORTED in ASP.NET etc. !

You can use use other libraries like OpenXML from Microsoft (free) or Aspose.Words (commercial) which do not rely on Office and are fully supported in server-scenarios like yours.

Yahia
  • 69,653
  • 9
  • 115
  • 144
0

If uploaded files exists in directory then instead of using httpfilecollection use Directory.GetFiles. It will return specific files in your case *.doc files from directory and then convert it to .html.

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