我的服务器中有一个Word文档,我想发送给我的客户。实际上,我希望他们下载该文件。我正在运行时创建该文件,当他们从我的服务器下载该文件后,我想将其删除。我正在本地尝试这种情况。创建文件后,我的服务器将其发送给客户端。在网络浏览器中,我看到以下内容:
我不要这个我希望Web浏览器打开“保存文件”对话框。我希望客户端下载真实文件。这是我的代码:
Guid temp = Guid.NewGuid();
string resultFilePath = Server.MapPath("~/formats/sonuc_" + temp.ToString() + ".doc");
if (CreateWordDocument(formatPath, resultFilePath , theLst)) {
Response.TransmitFile(resultFilePath);
Response.Flush();
System.IO.File.Delete(resultFilePath);
Response.End();
}
最佳答案
该代码段应该可以解决问题,但是请注意,这将导致将整个文件加载到(服务器的)内存中。
private static void DownloadFile(string path)
{
FileInfo file = new FileInfo(path);
byte[] fileConent = File.ReadAllBytes(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", file.Name));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.BinaryWrite(fileConent);
file.Delete();
HttpContext.Current.Response.End();
}
关于c# - 发送文件到客户端并删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22778178/