如何重写文件路径或正确设置虚拟路径?

时间:2009-07-01 02:17:30

标签: c# visual-studio rewrite mime

我想在我的网站上编写/img.png并重写E:/something/else/root/img.png的路径。

我可以重写路径(并使用File.Exist验证它是否存在),但是服务器找不到图像。我正在使用传输文件,但这会导致mime问题。

如何设置虚拟路径?我正在使用视觉工作室9(2008)。

2 个答案:

答案 0 :(得分:0)

您可以使用此处理程序从服务器上的某个位置服务器映像文件:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.IO;

    namespace TestWeb
    {
        /// <summary>
        /// Summary description for $codebehindclassname$
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        public class ServeImage : IHttpHandler
        {
            private static IDictionary<string, string> contentTypes = 
                                new Dictionary<string, string>();

            static ServeImage()
            {
                    contentTypes.Add("pdf","Content-type: application/pdf");
                    contentTypes.Add("gif","image/gif");
                    contentTypes.Add("png","image/x-png");
                    contentTypes.Add("jpg","image/jpeg");
                    contentTypes.Add("jpeg","image/jpeg");
                    contentTypes.Add("jpe","image/jpeg");
                    contentTypes.Add("tiff","image/tiff");
                    contentTypes.Add("tif","image/tiff");
                    contentTypes.Add("rtf","application/rtf");
                    contentTypes.Add("doc","application/msword");
                    contentTypes.Add("zip","application/zip");
                    contentTypes.Add("rar","applicatin/rar");
                    contentTypes.Add("ppz","application/mspowerpoint");
                    contentTypes.Add("ppt","applicaiton/vnd.ms-mspowerpoint");
                    contentTypes.Add("pps","applicaiton/vnd.ms-mspowerpoint");
                    contentTypes.Add("pot","applicaiton/vnd.ms-mspowerpoint");
                    contentTypes.Add("xls","application/x-msexcel");
                    contentTypes.Add("xlsx","application/x-msexcel");
            }


            public void ProcessRequest(HttpContext context)
            {
                ServeFile(context);
            }
            private void ServeFile(HttpContext context)
            {
                string serverAdd = context.Server.MapPath("/");
                string file = context.Request.QueryString["img"] + "";
                //suppose your url is http://host/ServeImage.isr?img=pic.png
                //OR http://host/ServeImage.isr?img=images/somepic.png

                file = file.Replace("/", "\\");
                //as MapPath will return path separated with \

                if (File.Exists(serverAdd + file))
                {
                    FileInfo fi = new FileInfo(serverAdd + file);

                    FileStream fs=fi.OpenRead();
                    byte[] ar = new byte[fs.Length];
                    int len=1024;
                    int j=0;
                    int i = fs.Read(ar, 0, len);                
                    while (i > 0)
                    {
                        j += Math.Min(len, i);

                        if (j + len > ar.Length)
                            len = ar.Length-j;

                        i = fs.Read(ar, j, len);
                    }

                    HttpResponse response = context.Response;
                    string ext = Path.GetExtension(file);

                    response.Clear();
                    if (contentTypes.ContainsKey(ext))
                        response.AppendHeader("contentType", 
                                      contentTypes[ext]);

                    response.Buffer = true;
                    response.BinaryWrite(ar);
                    response.Flush();
                    response.End();
                }
            }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

在Web.Config中:

<httpHandlers>
  ......
  <add verb="*" path="*.isr" validate="false" 
                 type="TestWeb.ServeImage, TestWeb" />
  ......
</httpHandlers>    

在HTML中:

 <a href="/GetImage.isr?img=images/DSC00009.jpg" target="_blank">Image 1</a>

答案 1 :(得分:0)

听起来像你想使用MapPath:

protected void Page_Load(object sender, EventArgs args)
{
    string pathToFile = Server.MapPath("/img.png");
    if (File.Exists(pathToFile))
        ...
}