我有以下方法,它将HTML转换为Word文档并将其作为下载发送给用户。
public static void HtmlToWordDownload(string HTML, string FileName, string title = "", bool border = false)
{
lock (LockMulti)
{
string strBody = string.Empty;
strBody = @"<html xmlns:o='urn:schemas-microsoft-com:office:office' " +
"xmlns:w='urn:schemas-microsoft-com:office:word'" +
"xmlns='http://www.w3.org/TR/REC-html40'>" +
"<head><title>:" + title + "</title>" +
"<!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:Zoom>100</w:Zoom>" +
"<w:DoNotOptimizeForBrowser/></w:WordDocument></xml><![endif]-->" +
"<style> @page Section1 {size:8.27in 11.69in; mso-first-footer:ff1; mso-footer: f1; mso-header: h1; " +
((border == true) ? "border:solid navy 2.25pt; padding:24.0pt 24.0pt 24.0pt 24.0pt; " : "") +
"margin:0.6in 0.6in 0.6in 0.6in ; mso-header-margin:.1in; " +
"mso-footer-margin:.1in; mso-paper-source:0;} " +
"div.Section1 {page:Section1;} p.MsoFooter, li.MsoFooter, " +
"div.MsoFooter{margin:0in; margin-bottom:.0001pt; " +
"mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; " +
"font-size:12.0pt; font-family:'Arial';} " +
"p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0in; " +
"margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center " +
"3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}--></style></head> ";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + FileName + ".doc");
StringBuilder htmlCode = new StringBuilder();
htmlCode.Append(strBody);
htmlCode.Append("<body><div class=Section1>");
htmlCode.Append(HTML);
htmlCode.Append("</div></body></html>");
HttpContext.Current.Response.Write(htmlCode.ToString());
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
}
}
现在我不想直接将其下载给用户,我想首先将其保存在我服务器的本地文件夹中,然后将其下载。我该怎么做?
答案 0 :(得分:0)
您可能会尝试在执行htmlCode.ToString()时提供您生成的文件:
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
另一种方法是保存文件并将其作为字节数组读取并按如下方式提供:
byte[] Content= File.ReadAllBytes(FilePath); //missing ;
Response.ContentType = "text/csv";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".csv");
Response.BufferOutput = true;
Response.OutputStream.Write(Content, 0, Content.Length);
Response.End();
或
string filename="Connectivity.doc";
if (filename != "")
{
string path = Server.MapPath(filename);
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
else
{
Response.Write("This file does not exist.");
}
}
,否则强>
在某个临时路径上将word / pdf文件保存在服务器上后,可以使用HTTP Handler(.ashx)下载文件,如下所示:
ExamplePage.ashx:
public class DownloadFile : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName + ";");
response.TransmitFile(Server.MapPath("FileDownload.csv"));
response.Flush();
response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
然后,您可以通过按钮单击事件处理程序调用HTTP Handler,如下所示:
标记:
<asp:Button ID="btnDownload" runat="server" Text="Download File"
OnClick="btnDownload_Click"/>
代码隐藏:
protected void btnDownload_Click(object sender, EventArgs e)
{
Response.Redirect("PathToHttpHandler/DownloadFile.ashx");
}
将参数传递给HTTP处理程序:
您可以简单地将查询字符串变量附加到Response.Redirect(),如下所示:
Response.Redirect("PathToHttpHandler/DownloadFile.ashx?yourVariable=yourValue");
然后在实际的处理程序代码中,您可以使用HttpContext中的Request对象来获取查询字符串变量值,如下所示:
System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
string yourVariableValue = request.QueryString["yourVariable"];
// Use the yourVariableValue here
注意 - 通常将文件名作为查询字符串参数传递给用户建议文件实际是什么,在这种情况下,他们可以使用另存为覆盖该名称值...