110,535
社区成员
发帖
与我相关
我的任务
分享
object content = string.Empty;
string commandArgument = e.CommandArgument.ToString();
string filePath = commandArgument;
string fileext = System.IO.Path.GetExtension(filePath);
string contentType = string.Empty;
if (fileext == ".doc" || fileext == ".docx")
{
_Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
_Document myWordDoc;
contentType = "application/msword";
myWordDoc = myWordApp.Documents.Open(Constant.GetInnerFileServerPath() + filePath);
content = myWordDoc.Content.Text;
myWordDoc.Close();
myWordApp.Quit();
}
else if (fileext == ".pdf")
{
StreamReader sr = null;
Stream ostream = HttpWebResponseUtility.CreateGetHttpResponse(Constant.GetInnerFileServerPath() + filePath, 2000, null, null).GetResponseStream();
sr = new StreamReader(ostream, System.Text.Encoding.UTF8);
string strContent= sr.ReadToEnd();
content = strContent;
contentType = "application/pdf";
}
Response.AppendHeader("Connection", "close");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(filePath), System.Text.Encoding.UTF8));
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = contentType;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.Write(content);
Response.Flush();
Response.End();
Stream ostream = HttpWebResponseUtility.CreateGetHttpResponse(Constant.GetInnerFileServerPath() + filePath, 2000, null, null).GetResponseStream();
List<byte> bytes = new List<byte>();
int temp = ostream.ReadByte();
while (temp != -1)
{
bytes.Add((byte)temp);
temp = ostream.ReadByte();
}
contentType = "application/octet-stream";
Response.AppendHeader("Connection", "close");
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(filePath), System.Text.Encoding.UTF8));
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = contentType;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.BinaryWrite(bytes.ToArray());
Response.Flush();
Response.End();