Asp.net中FileUpload控件的默认文件夹
本文关键字:默认 文件夹 控件 FileUpload net Asp | 更新日期: 2023-09-27 18:21:38
我正在尝试将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();
}
您的代码实际上很好——您只需要将其添加到一个常规方法中,该方法将在页面加载时调用(或在其他任何您想要的地方)。我可能仍然会点击按钮,这样它就不会在每次点击或刷新页面时都会熄灭。您只需要更改到服务器上某个位置的路径。在所有地方都有一个地图路径,为其提供文档文件夹的服务器位置。为了清晰起见,我删除了所有不需要更改的代码行。
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" />