『壹』 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我沒配置。
我不知道你那是什麼樣的問題,但是像我這樣是沒有問題的。