2012-01-28 4 views
0

コンテンツ/ギャラリーフォルダにギャラリー画像をアップロードし、コンテンツ/ファイルフォルダにダウンロード可能なファイルをアップロードするには、fileupload.csを再利用できるようにする必要があります。私は解決策がファイルパス文字列を何とか動的にすることになると考えています。ギャラリーとファイルには異なるコントローラ、モデル、ビューがあります。以下はfileupload.csコードがあるファイルアップロードのFilePathを制御するMVC3

public static class FileUpload 

{ 
    public static char DirSeparator = System.IO.Path.DirectorySeparatorChar; 
    public static string FilesPath = "Content" + DirSeparator + "Files" + DirSeparator; 
    public static string UploadFile(HttpPostedFileBase file) 
    { 
     // Check if we have a file 
     if (null == file) return ""; 
     // Make sure the file has content 
     if (!(file.ContentLength > 0)) return ""; 
     string fileName = file.FileName; 
     string fileExt = Path.GetExtension(file.FileName); 
     // Make sure we were able to determine a proper 
     // extension 
     if (null == fileExt) return ""; 
     // Check if the directory we are saving to exists 
     if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory+FilesPath)) 
     { 
     // If it doesn't exist, create the directory 
      Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory+FilesPath); 
     } 
     // Set our full path for saving 
     string path = FilesPath + DirSeparator + fileName; 
     // Save our file 
     //file.SaveAs(Path.GetFullPath(path)); 
     file.SaveAs(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory +path)); 
     // Return the filename 
     return fileName; 
    } 
    public static void DeleteFile(string fileName) 
    { 
    // Don't do anything if there is no name 
     if (fileName.Length == 0) return; 
    // Set our full path for deleting 
     string path = FilesPath + DirSeparator + fileName; 
    // Check if our file exists 
     if (File.Exists(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory+path))) 
     { 
      // Delete our file 
      File.Delete(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory+path)); 
     } 
    } 
} 
+1

あなたはこれまでに何を試しましたか?何がうまくいかなかったのですか? 90%であなたの要件を満たし、他の誰かが10%をやるように求めるインターネット上のどこかのソースコードを表示するのは、Stack Overflowのためのものではありません。 'あなたは、ファイルパス文字列を何らかの形で動的にするという解決策が得られると信じていると言っています。これは素晴らしいことです。この最初のアイデアをより正確にいかに試しましたか? –

答えて

0

アップロードされたファイルがkonwnしている場合は、画像形式の拡張子(JPG、GIF、PNGなど)を確認することができますので、あなたのコンテンツ/ギャラリーフォルダに保存する場合はそうでない場合は、あなたのコンテンツを保存します/ filesフォルダー。

とにかく

string fileExt = Path.GetExtension(file.FileName); 

ファイルetensionをチェックしているが、あなたはそれを使用することはありません。

+0

@ korzenlow方向性ありがとう – Diin

関連する問題