『壹』 ASP中如何读取文件夹的名字
<%
infopath=request.servervariables("path_info") '得到文件相对路径
serverpath=server.mappath("要循环的文件夹/文件夹下的文件") '得到文件绝对路径 这个我没改。必须的要在循环的文件夹下面建一个文件。并在上面写上才可以。
set objfso=createobject("scripting.filesystemobject") '实例文件组件
set objfile=objfso.GetFile(serverpath) '读取文件所在路径
set objfolder=objfile.parentfolder '根据文件所在路径得到上级目录
%>
<%
for each objfoldercount in objfolder.subfolders '循环显示文件夹
response.write(objfoldercount.name)
next
%>
也可以这样
serverpath=server.mappath("要循环的文件夹/文件夹下的文件")
split(serverpath,"/")
之取数组的值就可以了,那就是文件夹
『贰』 在asp.net mvc3 中,在controller中使用HttpPostedFileBase file 参数获取上传的文件,文件路径问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcTest1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "欢迎使用 ASP.NET MVC!";
return View();
}
//上传文件的控件name是file1,也就是<input type="file" name="file1" />
//上传到Upload文件夹(与Controllers文件夹同级)
[HttpPost]
public ActionResult About()
{
HttpFileCollectionBase files= Request.Files;
HttpPostedFileBase file = files["file1"];//获取上传的文件
if (file != null && file.ContentLength > 0)
{
string path = Server.MapPath("~/Upload/");//获取uplaod文件夹路径
try
{
file.SaveAs(path + file.FileName);//保存文件
}
catch (Exception e)
{
throw e;
}
}
else
{
//文件为空的处理
}
return View();
}
}
}
这是我的代码,我试了,可以上传成功的,当然不能大于4M,因为web.config我没配置。
我不知道你那是什么样的问题,但是像我这样是没有问题的。