2013-04-17 88 views
1

我試圖上傳多個文件,並通過將它們放入for循環同時將它們轉換爲.html格式。多個文件上傳完成並存儲到「上傳」文件中,但只有第一個文件被轉換爲.htm格式,而不是全部。多文件上傳並將其轉換爲html格式

這裏是我的代碼:

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); 
       } 
      } 
     } 
    } 

回答

0

使用asp.net一個文件上傳控件無法上傳多個文件。

檢查這裏的解決方案:asp.net multiple uploads with multiple fileupload controlhttp://www.dotnetcurry.com/ShowArticle.aspx?ID=68

或上傳多個文件,您可以在客戶端生成多個上傳控件或創建用戶定義的控制。

+0

我初學者到C#...所以,如果你能指出什麼錯誤,我的代碼做了.. – user1593699

+0

上傳他們是成功的,並保存在文件夾「上傳」但只有第一個doc文件被轉換成.htm格式 – user1593699

0

如果上傳的文件存在於目錄中,那麼使用Directory.GetFiles而不是使用httpfilecollection。它將從目錄中返回您的案例* .doc文件中的特定文件,然後將其轉換爲.html。

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