如何从c#.net中的网页编写和下载文件 [英] How to write and download a file from webpage in c#.net

查看:98
本文介绍了如何从c#.net中的网页编写和下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个文本文件并下载该文件.我使用了以下代码,但不起作用:

I need to write a text file and download that file. I used this following code, but it is not working :

string f1 = Path.GetTempPath() + "Activity.txt";
            FileInfo f2 = new FileInfo(f1);
            if (File.Exists(f2.Name))
            {
                File.Delete(f1);
            }
            StreamWriter sw = File.CreateText(Path.GetTempPath() + "Activity.txt");
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine("\t" + "\t" + "\t\t\Binderab City");
           
          
            sw.Close();
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment; filename=" + f2.Name);
            Response.AddHeader("content-length", f2.Length.ToString());
            Response.ContentType = "Application/Org.txt";
            Response.WriteFile(f2.FullName);
            Response.End();
            Response.Flush();

推荐答案

写入"和下载"文件是互斥的.这是一个客户端部分,用于下载内容.我从您的代码中看到的是不同的:响应客户端的HTTP请求,您的服务器端将编写并创建临时文件,然后将该文件以HTTP请求的形式发送给客户端,以下载该文件.

原则上,该代码应该工作—差不多,但是充满了一些荒谬之处.没有此类内容类型.您可能的意思是文本/纯文本". 真正的问题是:您不处置 StreamWriter,因此您不能保证文件已保存;最好使用using语句(不要与using子句混淆!).获取FullName是多余的.

首先,因为这只不过是一个可以使用ASCII编码传输的简单文本文件,所以您甚至不需要文件.您可以在回复中直接写:
"Write" and "download" a file are mutually exclusive. It''s a client part which downloads something. What I can see from your code is different: in response to client''s HTTP request, your server side writes and creates temporary file, then this file is sent in HTTP request to the client which downloads it.

In principle, this code should work — almost, but it''s full of some absurdities. There is no such content types. You probably mean "text/plain". The real problem is: you don''t dispose StreamWriter, so you do not guarantee that your file is saved; it''s the best to use using statement (don''t mix up with using clause!). Getting FullName is redundant.

First of all, as this is nothing more than a simple text file which could be transferred in ASCII encoding, so you don''t even need a file. You could write directly in your response:
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=some-file.txt");
Response.ContentType = "text/plain";
Response.Write();
Response.Write();
Response.Write("\t" + "\t" + "\t\t\Binderab City");
Response.End();



这样,客户端始终可以下载某些文件,而实际上该文件实际上不是服务器端的文件.

当然,您可以编写文件,但是如果您想编写更复杂的文件,这是有道理的.另外,我将演示如何发送带有BOM的UTF-8编码的文件,因此您可以使用非ASCII Unicode代码点,并使用任何具有标准行为的Unicode文本编辑器读取下载的文本文件-识别编码按物料清单:



This way, a client side can always download some file, which is not physically a file on the server side.

You can write a file, of course, but it makes sense if you want to write something more complex. As a bonus, I''ll demonstrate sending a file with UTF-8 encoding with BOM, so you could use non-ASCII Unicode code points and read the downloaded text file with any Unicode-enabled text editor with standard behavior — recognition of encoding by BOM:

Response.ContentEncoding = System.Text.Encoding.UTF8;

string fileName = string.Format("{0}{1}", System.IO.Path.GetTempPath(), "Activity.txt");

using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName, false, Response.ContentEncoding)) {
    writer.WriteLine();
    writer.WriteLine("...");
} //Important! writer.Dispose is called automatically here

Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=some-file.txt");
Response.ContentType = "text/plain";
Response.Charset = "utf-8";
Response.WriteFile(fileName);
Response.End();
// and think about doing this, sooner or later:
System.IO.File.Delete(fileName);



另请参见:
http://en.wikipedia.org/wiki/Byte_order_mark [ http://unicode.org/faq/utf_bom.html [ http://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ].

—SA



See also:
http://en.wikipedia.org/wiki/Byte_order_mark[^],
http://unicode.org/faq/utf_bom.html[^],
http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

—SA


这篇关于如何从c#.net中的网页编写和下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆