问题是这样的:
我想查到某个项目某文件夹下的所有文件名,然后输出到浏览器。在vs中调试,可以看到文件名已经获取了,resp.write(;;;)也是执行了,但是浏览器就是获取不到信息。显示的是“服务器未发送任何数据”。甚至我在这个方法把最后要输出的结果换个“hello”这样的字符串都不行。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using zafu.lib;
using System.Collections;namespace service
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpResponse resp = context.Response;
HttpRequest req = context.Request;
string arg = req.QueryString["arg"];
switch (arg)
{
case "q":doGetDefImg(req,resp);break;
case "qex": doGetDefImgEx(context); break;
case "q2": doGetDefImg2(context); break;
case "test": resp.Write("test!"); break;
default: resp.Write("has no arg!"); break;
}
} //访问本地文件,输出相应信息
public void doGetDefImg(HttpRequest req, HttpResponse resp)
{
string strobj = req.QueryString["obj"];
string strname = req.QueryString["nm"];
string dirpath = HttpRuntime.AppDomainAppPath.ToString()+GlobalSetting.IMG_DATA_PATH + strobj + "/" + strname + "/";
string[] fnams = FileHelper.GetAllImgFileNames(dirpath);
string result = JSONHelper.toJson(fnams);
resp.Write(result);
resp.Close();
}
//访问本地文件,但不输出相关信息
public void doGetDefImgEx(HttpContext context)
{
HttpResponse resp = context.Response;
HttpRequest req = context.Request;
string strobj = req.QueryString["obj"];
string strname = req.QueryString["nm"];
string dirpath = HttpRuntime.AppDomainAppPath.ToString() + GlobalSetting.IMG_DATA_PATH + strobj + "/" + strname + "/";
HttpRuntime.AppDomainAppPath.ToString();
string[] fnams = FileHelper.GetAllImgFileNames(dirpath);
string result = JSONHelper.toJson(fnams);
//resp.Write(fnams[0].ToString());
resp.Write("Hello!");
resp.Close();
} //不访问本地文件,输出 ["abc.jpg","1123.gif"]
public void doGetDefImg2(HttpContext context)
{
HttpResponse resp = context.Response;
HttpRequest req = context.Request;
ArrayList fnames = new ArrayList();
fnames.Add("abc.jpg");
fnames.Add("1123.gif");
string result = JSONHelper.toJson(fnames);
resp.Write(result);
// resp.Write("hhhhhhhhh!");
} public bool IsReusable
{
get
{
return true;
}
}
}
}FileHelper(上面出现的访问本地文件的帮助类)代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Collections;namespace lib
{
public class FileHelper
{
public static void CreateFile(string fileName, string content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
byte[] data = new UTF8Encoding().GetBytes(content); fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
}
public static string[] GetAllFileNames(string direcPath,string[] types)
{
DirectoryInfo dir = new DirectoryInfo(direcPath);
ArrayList fnames = new ArrayList(20);
if (types == null || types.Length == 0)
foreach (var finfo in dir.GetFiles())
fnames.Add(finfo.Name);
else
{
foreach (var type in types)
{
FileInfo[] ftemps = dir.GetFiles(type);
foreach (var ftemp in ftemps)
{
fnames.Add(ftemp.Name);
}
}
}
return fnames.ToArray() as string[];
}
public static string[] GetAllImgFileNames(string direcPath)
{
return GetAllFileNames(direcPath,new string[]{"*.jpg","*.png","*.gif"});
} }
}
这个问题奇怪了,都被困好几天了。望不吝赐教啊。
我想查到某个项目某文件夹下的所有文件名,然后输出到浏览器。在vs中调试,可以看到文件名已经获取了,resp.write(;;;)也是执行了,但是浏览器就是获取不到信息。显示的是“服务器未发送任何数据”。甚至我在这个方法把最后要输出的结果换个“hello”这样的字符串都不行。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using zafu.lib;
using System.Collections;namespace service
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpResponse resp = context.Response;
HttpRequest req = context.Request;
string arg = req.QueryString["arg"];
switch (arg)
{
case "q":doGetDefImg(req,resp);break;
case "qex": doGetDefImgEx(context); break;
case "q2": doGetDefImg2(context); break;
case "test": resp.Write("test!"); break;
default: resp.Write("has no arg!"); break;
}
} //访问本地文件,输出相应信息
public void doGetDefImg(HttpRequest req, HttpResponse resp)
{
string strobj = req.QueryString["obj"];
string strname = req.QueryString["nm"];
string dirpath = HttpRuntime.AppDomainAppPath.ToString()+GlobalSetting.IMG_DATA_PATH + strobj + "/" + strname + "/";
string[] fnams = FileHelper.GetAllImgFileNames(dirpath);
string result = JSONHelper.toJson(fnams);
resp.Write(result);
resp.Close();
}
//访问本地文件,但不输出相关信息
public void doGetDefImgEx(HttpContext context)
{
HttpResponse resp = context.Response;
HttpRequest req = context.Request;
string strobj = req.QueryString["obj"];
string strname = req.QueryString["nm"];
string dirpath = HttpRuntime.AppDomainAppPath.ToString() + GlobalSetting.IMG_DATA_PATH + strobj + "/" + strname + "/";
HttpRuntime.AppDomainAppPath.ToString();
string[] fnams = FileHelper.GetAllImgFileNames(dirpath);
string result = JSONHelper.toJson(fnams);
//resp.Write(fnams[0].ToString());
resp.Write("Hello!");
resp.Close();
} //不访问本地文件,输出 ["abc.jpg","1123.gif"]
public void doGetDefImg2(HttpContext context)
{
HttpResponse resp = context.Response;
HttpRequest req = context.Request;
ArrayList fnames = new ArrayList();
fnames.Add("abc.jpg");
fnames.Add("1123.gif");
string result = JSONHelper.toJson(fnames);
resp.Write(result);
// resp.Write("hhhhhhhhh!");
} public bool IsReusable
{
get
{
return true;
}
}
}
}FileHelper(上面出现的访问本地文件的帮助类)代码如下using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Collections;namespace lib
{
public class FileHelper
{
public static void CreateFile(string fileName, string content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
byte[] data = new UTF8Encoding().GetBytes(content); fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
}
public static string[] GetAllFileNames(string direcPath,string[] types)
{
DirectoryInfo dir = new DirectoryInfo(direcPath);
ArrayList fnames = new ArrayList(20);
if (types == null || types.Length == 0)
foreach (var finfo in dir.GetFiles())
fnames.Add(finfo.Name);
else
{
foreach (var type in types)
{
FileInfo[] ftemps = dir.GetFiles(type);
foreach (var ftemp in ftemps)
{
fnames.Add(ftemp.Name);
}
}
}
return fnames.ToArray() as string[];
}
public static string[] GetAllImgFileNames(string direcPath)
{
return GetAllFileNames(direcPath,new string[]{"*.jpg","*.png","*.gif"});
} }
}
这个问题奇怪了,都被困好几天了。望不吝赐教啊。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货