小编Gen*_*smo的帖子

Response.TransmitFile() 的替代方案

所以我有一组我过去几天一直在玩的代码,我需要从服务器下载一个文件到客户端。这是最简单的部分,但我还需要在完成后刷新网格视图并在文件已成功创建的警报中显示,但我发现下载的每种方式都包含一个选择的代码行,这将是我的倒台。

Response.End()

Response.Close() 或

ApplicationInstance.CompleteRequest()

所有这些都会结束当前的响应,或者我相信在 ApplicationInstance 的情况下,它将页面的所有源代码刷新到我尝试下载的文本文件中。下面是我从服务器下载文件的代码片段,下面是下载我的文件的源代码。如果您有任何可以帮助解决这个永无止境的噩梦的东西,我们将不胜感激。

            //I brought everything together in an arraylist to write to file.
            asfinalLines = alLines.ToArray(typeof(string)) as string[];

            string FilePath = HttpContext.Current.Server.MapPath("~/Temp/");
            string FileName = "test.txt";

            // Creates the file on server
            File.WriteAllLines(FilePath + FileName, asfinalLines);

            // Prompts user to save file
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ClearContent();
            response.Clear();
            response.ContentType = "text/plain";
            response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
            response.TransmitFile(FilePath + FileName);
            response.Flush();

            // Deletes the file on server
            File.Delete(FilePath + FileName);

            response.Close();
Run Code Online (Sandbox Code Playgroud)

c# asp.net webforms download httpcontext

5
推荐指数
1
解决办法
2万
查看次数

标签 统计

asp.net ×1

c# ×1

download ×1

httpcontext ×1

webforms ×1