如何在asp.net中打开.txt扩展文件 [英] How to open the .txt extension file in asp.net

查看:65
本文介绍了如何在asp.net中打开.txt扩展文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从本地服务器(使用visual studio)运行应用程序时,我可以打开文本文件但是当我尝试从托管站点(IIS)打开相同的文本文件时,它没有打开(我已创建IIS上同一应用程序的虚拟目录。)

I can able to open the text file when i run the application from local server(with visual studio) but when i try to open the same text file from the hosted site( IIS ), it is not opening(I have created a virtual directory for the same application on IIS.)

string filepath = Server.MapPath(Request.ApplicationPath) + "\\ReleaseNotes\\ReleaseNote.txt";
TextWriter tw = new StreamWriter(filepath);
tw.WriteLine("Text file content");
tw.Close();
ProcessStartInfo pinfo = new ProcessStartInfo();
pinfo.FileName = filepath;
Process p = new Process();
p.StartInfo = pinfo;
p.Start();







请帮我解决这个问题.. ..i会很感激你。




Pleas help me out with this issue ....i will appreciate you.

推荐答案

C#代码在服务器而不是客户端执行。

它在测试中有效,因为它们是同一台机器,因此客户端和服务器可以使用相同的文件夹。现在,当您发布代码时,服务器和客户端是不同的PC,可能相距数千公里,并且出于安全原因,服务器根本无法访问客户端硬盘。



没有办法解决这个问题:如果服务器需要打开文件,它必须首先从客户端传输到服务器,然后打开 - 这必须由客户端完成,而不是服务器因为它根本无法访问客户端硬盘。



即使可以,您启动的流程也会在服务器上运行,而不是客户端,因此用户根本不会看到它 - 它会开始惹恼你的网络主机管理员但是......
C# code is executed at the server, not the client.
It worked in testing, because they were the same machine, so the same folders were available to the client and the server. Now, when you release the code, the server and the client are different PC, perhaps thousands of kilometers apart, and the Server cannot access the Client hard disk at all, for security reasons.

There is no way round this: if the server needs to open the file, it must be transferred from the client to the server first, and then opened - and that has to be done by the client, not the server as it can't access the client HDD at all.

Even if it could, the Process you started would run on the Server, not the Client, so the user would not see it at all - it would start to annoy the heck out of your web host administrator though...


I have solved this issue in another way.After writing the content into text file.I wrote the content of ReleaseNote.txt into the TextFile.aspx page again.This is something as follows within page load event of that page.

    protected void Page_Load(object sender, EventArgs e)
    {


        string filename = "ReleaseNote.txt";

        try
        {
            if (!Page.IsPostBack)
            {
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("content-disposition", "attachment; filename=" + filename);

                FileStream sourceFile = new FileStream(Server.MapPath("~/ReleaseNotes/" + filename), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                long FileSize;
                FileSize = sourceFile.Length;
                if (sourceFile.Length > 0)
                {
                    byte[] getContent = new byte[(int)FileSize];

                    sourceFile.Read(getContent, 0, (int)sourceFile.Length);
                    sourceFile.Close();
                    Response.BinaryWrite(getContent);
                }
                else
                {
                    Response.Write("File is Empty");
                }

                Response.End();
            }
        }
        catch (Exception ex)
        {
            Response.End();
        }



    }




I opened that TextFile.aspx page from code behind of some other page by writing the code something like this:
ClientScript.RegisterStartupScript(typeof(Page), "ErrorOccurred", "<script type='text/javascript'>window.open('ReleaseNotes/TextFile.aspx')</script>");


这篇关于如何在asp.net中打开.txt扩展文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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