2016-06-16 9 views
1

だから、私は、これはコードです 、ASP.netコアを使用してローカルパスにファイルを「アップロード」するチュートリアルを追っ:どのように拡張されたファイルのプロパティを読み取るために/ファイルのメタデータ

public IActionResult About(IList<IFormFile> files) 
    { 

     foreach (var file in files) 
     { 
      var filename = ContentDispositionHeaderValue 
          .Parse(file.ContentDisposition) 
          .FileName 
          .Trim('"'); 
      filename = hostingEnv.WebRootPath + [email protected]"\{filename}"; 

      using (FileStream fs = System.IO.File.Create(filename)) 
      { 
       file.CopyTo(fs); 
       fs.Flush(); 
      } 
     } 


     return View(); 
    } 

私が読みたいです以下のようなファイル(ファイルのメタデータ)の拡張プロパティ:

  • 名、
  • 著者、掲載
  • 日、
  • など

このファイルを使用してファイルをソートするには、Iformfileを使用する方法がありますか?

答えて

3

さらに多くのファイルメタデータにアクセスしたい場合は、.NETフレームワークがootbを提供します。サードパーティライブラリを使用する必要があります。 それ以外の場合は、独自のCOMラッパーを作成してその詳細にアクセスする必要があります。

純粋なC#サンプルについてはlinkを参照してください。ここで

ファイルのプロパティの読み取り方法の例:

が への "Windows/System32に" フォルダからのShell32.dllへの参照を追加してプロジェクトを

List<string> arrHeaders = new List<string>(); 
List<Tuple<int, string, string>> attributes = new List<Tuple<int, string, string>>(); 

Shell32.Shell shell = new Shell32.Shell(); 
var strFileName = @"C:\Users\Admin\Google Drive\image.jpg"; 
Shell32.Folder objFolder = shell.NameSpace(System.IO.Path.GetDirectoryName(strFileName)); 
Shell32.FolderItem folderItem = objFolder.ParseName(System.IO.Path.GetFileName(strFileName)); 


for (int i = 0; i < short.MaxValue; i++) 
{ 
    string header = objFolder.GetDetailsOf(null, i); 
    if (String.IsNullOrEmpty(header)) 
     break; 
    arrHeaders.Add(header); 
} 

// The attributes list below will contain a tuple with attribute index, name and value 
// Once you know the index of the attribute you want to get, 
// you can get it directly without looping, like this: 
var Authors = objFolder.GetDetailsOf(folderItem, 20); 

for (int i = 0; i < arrHeaders.Count; i++) 
{ 
    var attrName = arrHeaders[i]; 
    var attrValue = objFolder.GetDetailsOf(folderItem, i); 
    var attrIdx = i; 

    attributes.Add(new Tuple<int, string, string>(attrIdx, attrName, attrValue)); 

    Debug.WriteLine("{0}\t{1}: {2}", i, attrName, attrValue); 
} 
Console.ReadLine(); 

このコードを充実させてカスタムクラスを作成し、必要に応じて並べ替えを行うことができます。

がそこに多くの有料版がありますが、画像のメタデータにアクセスするたとえばWindowsApiCodePack

と呼ばれる無料の1があり、私はそれが

ShellObject picture = ShellObject.FromParsingName(file); 

var camera = picture.Properties.GetProperty(SystemProperties.System.Photo.CameraModel); 
newItem.CameraModel = GetValue(camera, String.Empty, String.Empty); 

var company = picture.Properties.GetProperty(SystemProperties.System.Photo.CameraManufacturer); 
newItem.CameraMaker = GetValue(company, String.Empty, String.Empty); 
+0

をサポートしていますそれはネットコア – daniherculano

+1

@daniherculanoとの互換性がないと思います。 NETコアはクロスプラットフォームです。拡張プロパティは、Windows32に特有のもので、shell32.dllで利用可能なすべての機能を含みます。 – nawfal

+0

これは単純化することができます。参照:https://stackoverflow.com/a/46648086/661933 – nawfal

関連する問題