从Asp.net中的文本框路径中单击按钮打开文件 [英] Open a file on the click of button from a textbox path in Asp.net

查看:46
本文介绍了从Asp.net中的文本框路径中单击按钮打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅以下链接:

点击此处查看atricle

推荐答案

请参阅此内容解。我现在已经测试了样品。如果你得到了帮助,请马克回答。



我硬编码了文件路径。你可以从文本框中获取它并分配给filePath变量。



这是Link Button Click事件。您必须使用System.IO和System .NET作为额外的库,这是用于Path.getExtention()和WebClient类。



Please See this solution. I have tested now with sample.Please Mark as answered if you got help from this.

I hardcoded the file path. You can get it from text box and assign to filePath variable.

This is Link Button Click event. You have to use System.IO and System .NET as extra libraries, which is for Path.getExtention() and WebClient Class.

using System.IO;
using System.Net;







protected void LinkButton1_Click(object sender, EventArgs e)
      {
          string filePath = "C:\\GIHAN\\Employee Profile - Senior Engineer Technology.docx";
          string fileExtention = Path.GetExtension(filePath);
          WebClient client = new WebClient();
          Byte[] buffer = client.DownloadData(filePath);
          Response.ContentType = ReturnExtension(fileExtention);

          Response.AddHeader("content-length", buffer.Length.ToString());
          Response.BinaryWrite(buffer);
      }





这是退货延期功能





Here is the Return Extension Function

private string ReturnExtension(string fileExtension)
        {
            switch (fileExtension)
            {
                case ".htm":
                case ".html":
                case ".log":
                    return "text/HTML";
                case ".txt":
                    return "text/plain";
                case ".doc":
                    return "application/ms-word";
                case ".tiff":
                case ".tif":
                    return "image/tiff";
                case ".asf":
                    return "video/x-ms-asf";
                case ".avi":
                    return "video/avi";
                case ".zip":
                    return "application/zip";
                case ".xls":
                case ".csv":
                    return "application/vnd.ms-excel";
                case ".gif":
                    return "image/gif";
                case ".jpg":
                case "jpeg":
                    return "image/jpeg";
                case ".bmp":
                    return "image/bmp";
                case ".wav":
                    return "audio/wav";
                case ".mp3":
                    return "audio/mpeg3";
                case ".mpg":
                case "mpeg":
                    return "video/mpeg";
                case ".rtf":
                    return "application/rtf";
                case ".asp":
                    return "text/asp";
                case ".pdf":
                    return "application/pdf";
                case ".fdf":
                    return "application/vnd.fdf";
                case ".ppt":
                    return "application/mspowerpoint";
                case ".dwg":
                    return "image/vnd.dwg";
                case ".msg":
                    return "application/msoutlook";
                case ".xml":
                case ".sdxl":
                    return "application/xml";
                case ".xdp":
                    return "application/vnd.adobe.xdp+xml";
                default:
                    return "application/octet-stream";
            }
        }


这篇关于从Asp.net中的文本框路径中单击按钮打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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