Asp.net中FileUpload控件的默认文件夹

时间:2015-11-12 18:13:00

标签: c# asp.net file-upload

我正在尝试将Office Word文档转换为HTML并在ASP.Net中的浏览器中显示它,所以我的代码工作正常,但我想做的唯一更改是我不想使用FileUpload控件,而不是我想将我的默认位置设置为:C:\ Mydocument \所以如何摆脱fileupload控件并设置我的默认位置如上所述?

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

1 个答案:

答案 0 :(得分:0)

您的代码实际上很好,您只需要将它添加到常规方法中,该方法将在页面加载时(或您想要的任何其他位置)调用。我可能仍会按下按钮,因此每次点击或刷新页面时都不会触发。您只需将路径更改为服务器上的某个位置即可。在所有地方你有一个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" />