2009-07-01 7 views
0

自分のサイトに/img.pngと書いて、E:/something/else/root/img.pngへのパスを書き直したい。ファイルパスを書き換えたり、仮想パスを正しく設定する方法は?

パスを書き換えても、ファイルが存在していることを確認するためにFile.Existを使用することはできますが、サーバーはイメージを見つけられません。私は送信ファイルを使用していますが、それはMIME問題を引き起こしています。

仮想パスを設定するにはどうすればよいですか?私はビジュアルスタジオ9(2008)を使用しています。

+0

私が理解しているのは、をページに入れ、画像を物理的な場所E:\ something \ else \ roo \ img.pngに配置したいということです。または、ユーザーにファイルをダウンロードするためのリンクを提供していますか? – TheVillageIdiot

+0

前者は、物理的な場所から取得します。 –

答えて

0

あなたは、サーバー上のいくつかの場所からサーバーのイメージファイルにこのハンドラを使用することができますweb.configファイル

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; 
       } 
      } 
     } 
    } 

:HTMLで

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

を:

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

サウンドあなたがMapPathを使いたいのと同じように:

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

フォルダ(/など)をデフォルトの物理パスにマップできますか? –

+0

あなたは何を意味するのか分かりません。Server.MapPath( "/")は、Webサイトを含むドライブ上のフォルダを提供します。 –

関連する問題