提示用户保存/ ASP.NET中的C#打开文件 [英] Prompt user to save/open file in ASP.NET C#

查看:155
本文介绍了提示用户保存/ ASP.NET中的C#打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不应该是这个很难找出如何做到这一点。基本上我试图把一个字符串,并让客户保存,当他们点击一个按钮。它应该弹出一个保存/打开对话框。没有多余的花俏或任何东西。这不是火箭科学(或因此我想过)。

似乎有一吨不同的方式,(StreamWriter的,Htt的presponse等),但没有我已经能够正确地找到工作或解释这是怎么回事的例子。先谢谢了。

这是例如code的很多块,我发现的一个...

(这只是一个例子,随意不垒解决这个答案。)

 字符串文件名=FILENAME.TXT;
字符串的文件路径=C:/ .... //替换此
System.Web.Htt presponse响应= System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType =text / plain的;
response.AddHeader(内容处置,附件;文件名=+文件名+;);
response.TransmitFile(文件路径);
response.Flush();
到Response.End();

2行表示替换字符串。怎么样?这code被宣传为造就了对话。我不应该不必设置在code的路径,对吧?

编辑:最后的结局(编辑再次,删除有来结束()之前;)

 字符串文件路径=使用Server.Mappath(〜/温度/);
        字符串文件名=的test.txt;        //创建在服务器上的文件
        File.WriteAllText(文件路径+文件名,你好);        //提示用户保存文件
        System.Web.Htt presponse响应= System.Web.HttpContext.Current.Response;
        response.ClearContent();
        response.Clear();
        response.ContentType =text / plain的;
        response.AddHeader(内容处置,附件;文件名=+文件名+;);
        response.TransmitFile(文件路径+文件名);
        response.Flush();        //删除服务器上的文件
        File.Delete(文件路径+文件名);        到Response.End();


解决方案

2(文件路径)行表示该文件的在服务器上的路径

8号线:

  response.TransmitFile(文件路径);

发送该特定文件给客户,这就是弹出保存对话框。

如果你不传输文件,我不知道如果对话框将在所有(即使你用了一个标题)弹出

不管怎么说,我觉得8号线应为:

  response.TransmitFile(文件路径+文件名);

It shouldn't be this hard to find out how to do this. Basically I'm trying to take a string and let the client save it when they click a button. It should pop up with a Save/Open dialog. No extra bells and whistles or anything. It's not rocket science, (or so I would've thought).

There seems to be a ton of different ways, (StreamWriter, HttpResponse, etc.), but none of the examples I've been able to find work properly or explain what's going on. Thanks in advance.

An example one of the many blocks of code I've found...

(This is just an example, feel free to not base your answer around this.)

String FileName = "FileName.txt";
String FilePath = "C:/...."; //Replace this
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(FilePath);
response.Flush();
response.End();

Line 2 says to replace that string. How? This code was advertised as bringing up a dialog. I shouldn't be having to set a path in the code, right?

EDIT: Final Outcome (Edited again, Delete has to come before End();)

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

        // Creates the file on server
        File.WriteAllText(FilePath + FileName, "hello");

        // Prompts user to save file
        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(FilePath + FileName);
        response.Flush();

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

        response.End();

解决方案

Line 2 (FilePath) indicates the path to the file on the server

Line 8:

response.TransmitFile(FilePath);

Transmits that specific file to the client and THAT is what pops the save dialog.

If you don't transmit the file, I'm not sure if the dialog will pop up at all (even though you set a header)

Anyways, I think line 8 should read:

    response.TransmitFile(FilePath + FileName);

这篇关于提示用户保存/ ASP.NET中的C#打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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