簡體   English   中英

Asp.net 中 FileUpload 控件的默認文件夾

[英]Default folder for the FileUpload Control in Asp.net

我正在嘗試將 Office Word 文檔轉換為 HTML 並在 ASP.Net 的瀏覽器中顯示它,所以我下面的代碼工作正常,但我想要做的唯一更改是我不想使用 FileUpload 控件,而是我想將我的默認位置設置為簡單如下:C:\\Mydocument\\ 那么我怎樣才能擺脫文件上傳控件並像上面一樣設置我的默認位置?

 protected void Upload(object sender, EventArgs e)
    {
      object missingType = Type.Missing;
      object readOnly = true;
      object isVisible = false;
      object documentFormat = 8;
      string randomName = DateTime.Now.Ticks.ToString();
      object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";
      string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";

      //Upload the word document and save to Temp folder
      FileUpload1.SaveAs(Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
      object fileName = FileUpload1.PostedFile.FileName;

      //Open the word document in background
      ApplicationClass applicationclass = new ApplicationClass();
      applicationclass.Documents.Open(ref fileName,
                                      ref readOnly,
                                      ref missingType, ref missingType, ref missingType,
                                      ref missingType, ref missingType, ref  missingType,
                                      ref missingType, ref missingType, ref isVisible,
                                      ref missingType, ref missingType, ref missingType,
                                      ref missingType, ref missingType);
      applicationclass.Visible = false;
      Document document = applicationclass.ActiveDocument;

      //Save the word document as HTML file
      document.SaveAs(ref htmlFilePath, ref documentFormat, ref missingType,
                      ref missingType, ref missingType, ref missingType,
                      ref missingType, ref missingType, ref missingType,
                      ref missingType, ref missingType, ref missingType,
                      ref missingType, ref missingType, ref missingType,
                      ref missingType);

      //Close the word document
      document.Close(ref missingType, ref missingType, ref missingType);

      //Delete the Uploaded Word File
      File.Delete(Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName));

      //Read the Html File as Byte Array and Display it on browser
      byte[] bytes;
      using (FileStream fs = new FileStream(htmlFilePath.ToString(), FileMode.Open, FileAccess.Read))
      {
        BinaryReader reader = new BinaryReader(fs);
        bytes = reader.ReadBytes((int)fs.Length);
        fs.Close();
      }
      Response.BinaryWrite(bytes);
      Response.Flush();

      //Delete the Html File
      File.Delete(htmlFilePath.ToString());
      foreach (string file in Directory.GetFiles(directoryPath))
      {
        File.Delete(file);
      }
      Directory.Delete(directoryPath);
      Response.End();
    }

您的代碼實際上很好,您只需要將其添加到將在頁面加載時(或您想要的任何其他地方)調用的常規方法。 我可能仍然會在單擊按鈕時使用它,因此每次點擊或刷新頁面時它都不會觸發。 您只需要更改指向服務器上某個位置的路徑。 在所有地方,您都有一個 mappath 為其提供文檔文件夾的服務器位置。 為了清楚起見,我刪除了所有不需要更改的代碼行。

protected void Upload(object sender, EventArgs e)
{
     var docPath = Server.MapPath("/yoursiteroot/documents/"); //your web server folder containing documents

      object htmlFilePath = docPath + randomName + ".htm";
      string directoryPath = docPath + randomName + "_files";

      //Upload the word document and save to Temp folder
      FileUpload1.SaveAs(docPath + Path.GetFileName(FileUpload1.PostedFile.FileName));
      object fileName = FileUpload1.PostedFile.FileName;

      //Delete the Uploaded Word File
      File.Delete(docPath + Path.GetFileName(FileUpload1.PostedFile.FileName));
}

<asp:Button ID="btnConvert" runat="server" text="Convert To HTML" click="Upload" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM