利用SlickUpload上传文件

利用Session变量解决勇在LetterFolderCustomFileNameGenerator.cs文件内的GenerateFileName()函数动态碟成如 "P00001" "P01009"等样式的目录

 public string GenerateFileName(UploadedFile file)
  {
   //string fileName = Path.Combine(file.ClientName.Substring(0, 1).ToLower(), file.ClientName);
   //DirectoryInfo di = Directory.CreateDirectory(@"d:cnawebjournal-center.comXYZ");
   //CreateDirectory(HttpContext.Current.Server.MapPath("../") + HttpContext.Current.Request.QueryString["PaperIDID"]);
   
   CreateDirectory(HttpContext.Current.Server.MapPath("../") + HttpContext.Current.Session["FolderName"]);
   string fileName = Path.Combine((string)HttpContext.Current.Session["FolderName"], file.ClientName);
   return fileName;
  }

在LetterFolderCustomFileNameGenerator.cs文件中直接使用Session["FolderName"]会发生错误,

在文章http://dotnetspider.com/Question696.aspx找到答案

Hi All
 i m new to asp.net, any help will be appriciated ,
i have a login form where i get login/pass if success i generate two sessions like
Session["EmailAddress"] = EmailAddress;
Session["Name"] = Name;
upto here its pretty ok .. i have a .cs class naming user.cs it has different methods like AdUser,updateUser etc

the problem is that when ever i try to get the vale of session in this class it gives me this error : The name 'Session' does not exist in the class or namespace 'RMS.User'
y the session varible is not visible here ? or what should i do to get its value

Waiting 4 reply,
Qazi Asim

use this in your class files to access the session:
HttpContext.Current.Session("EmailAddress")

Example:


public class user
{
   public void MySessionValues()
   {
       string myEmailAddress = HTTPContext.Current.Session["EmailAddress"];
   }
}

利用SlickUpload上传文件的问题

By using the CreateDirectory() function provided in the following artical

http://www.codeproject.com/csharp/CreateDirectorymethod.asp

now, we can create a new folder for uploading files.

in LetterFolderCustomFileNameGenerator.cs, we add the function public static void CreateDirectory(string DirectoryPath)

public class LetterFolderCustomFileNameGenerator : ICustomFileNameGenerator
 {
  public string GenerateFileName(UploadedFile file)
  {
   //string fileName = Path.Combine(file.ClientName.Substring(0, 1).ToLower(), file.ClientName);
   //DirectoryInfo di = Directory.CreateDirectory(@"d:\cna\web\journal-center.com\XYZ");
   CreateDirectory("d:\\cna\\web\\journal-center.com\\" + "XYZNow");
   string fileName = Path.Combine("XYZNow", file.ClientName);
   return fileName;
  }

  public static void CreateDirectory(string DirectoryPath)
  {
   // trim leading \ character
   DirectoryPath = DirectoryPath.TrimEnd(Path.DirectorySeparatorChar);
   Scripting.FileSystemObject fso = new Scripting.FileSystemObjectClass();
   // check if folder exists, if yes - no work to do
   if(!fso.FolderExists(DirectoryPath))
   {
    int i = DirectoryPath.LastIndexOf(Path.DirectorySeparatorChar);
    // find last\lowest folder name
    string CurrentDirectoryName = DirectoryPath.Substring(i+1,
     DirectoryPath.Length-i-1);
    // find parent folder of the last folder
    string ParentDirectoryPath = DirectoryPath.Substring(0,i);
    // recursive calling of function to create all parent folders
    CreateDirectory(ParentDirectoryPath);
    // create last folder in current path
    Scripting.Folder folder = fso.GetFolder(ParentDirectoryPath);
    folder.SubFolders.Add(CurrentDirectoryName);
   }
  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值