利用SlickUpload上傳檔案

來源:互聯網
上載者:User

利用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);
   }
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.