不在iis中图片显示方法(文件同理)

问题:文件存放位置不在iis同一目录下,怎么去下载呢?

解决办法:

环境:asp.net,c#

1、创建一般处理程序,采用Response.WriteFile(物理路径)返回。

 var methodName = context.Request["method"];
        switch (methodName)
        { 
            case "getfile":
                getFile(context);
                break; 
        }
 /// <summary>
    /// get图片文件
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public void getFile(HttpContext context )
    {
        string  userID = context.Request["userid"];//传送图片名称
        string newFileName = userID+".jpg" ;
        string path =  @"e:\Test\userupfiles\";
        string realfilepath = path+ newFileName;
        if (  File.Exists (realfilepath ))
        {
        context.Response.WriteFile(realfilepath);
        }
        else
        {
      context.Response.WriteFile("images/no.jpg");
        }

        context.Response.End();
    }

2、前端图片显示方法

  <img src="Handler.ashx?method=getfile&userid=test" />


版权声明:本文为zhgl7688原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。