پیدا کردن نوع فایل در mvc

یکشنبه 3 آبان 1394

در این مقاله مامی خواهیم در mvc یک سند از html اپلود کنیم و نوع فایل را بدست اوریم. یک شماره در فایل است که فرمت ان بر مبنای 16 می باشد .

پیدا کردن نوع  فایل در mvc

 یک شماره تعبیه شده در فایل است که فرمت فایل را بر مبنای 16 در خود جای داده است. 

در زیر چارت Magic Number را نشان میدیم

ابتدا یک برنامه ویژوال استودیو از نوع mvc به صورت Empty ایجاد کنید.

سپس یک کنترلر جدید ایجاد کنید.

حال view مرتبط با action را ایجاد نمایید. 

کد زیر را به پروژه اضافه نمایید

<h2>Find File Type From Magic Number of File</h2>  
@using (Html.BeginForm("Index",  
"File_Upload",  
FormMethod.Post,  
new { enctype = "multipart/form-data" }))  
{  
   <label for="file">Upload file :</label>  
   <input type="file" name="file" id="file" /><br>  
   <input type="submit" id="submit" /> <br />  
   <p>Program_Errors will show here:</p>  
   @ViewBag.error  
   <p>You have uploaded file type is : @ViewBag.File_Type </p>  
   <br />  
   @ViewBag.Message  
   <br />  
   @ViewBag.signature  
   <br />  
   @ViewBag.out_put  
}  

حالا action دیگری را ایجاد نمایید با همان نام index و از روش post استفاده میکنیم و کد زیر را در ان قرار میدهیم

ما نیاز به اضافه کردن فضای نام system.io داریم.

[HttpPost]  
  
public ActionResult Index(HttpPostedFileBase file)  
{  
  
    string ext = null;  
  
    string[] file_hexa_signature = {  
        "38-42-50-53", "25-50-44-46", "4D-4D-00-2A", "4D-4D-00-2A"  
    };  
  
    try  
  
    {  
  
        ext = System.IO.Path.GetExtension(file.FileName).ToLower();  
  
    } catch (Exception ex)  
  
    {  
  
        ViewBag.error = ex.Message;  
  
    }  
  
    if (ext == null)  
  
    {  
  
        ViewBag.error = "please select onlt psd, pdf, tif file types only";  
  
    }  
  
  
    if (file != null && file.ContentLength > 0)  
  
    {  
  
        string str = Path.GetFileName(file.FileName);  
  
        string path = Path.Combine(Server.MapPath("~/Docs"),  
  
            Path.GetFileName(file.FileName));  
  
        file.SaveAs(path);  
  
        BinaryReader reader = new BinaryReader(new FileStream(Convert.ToString(path), FileMode.Open, FileAccess.Read, FileShare.None));  
  
        reader.BaseStream.Position = 0x0; // The offset you are reading the data from  
  
        byte[] data = reader.ReadBytes(0x10); // Read 16 bytes into an array  
  
        string data_as_hex = BitConverter.ToString(data);  
  
        reader.Close();  
  
        // substring to select first 11 characters from hexadecimal array  
  
        string my = data_as_hex.Substring(0, 11);  
  
        string output = null;  
  
        switch (my)  
  
        {  
  
            case "38-42-50-53":  
  
                output = " => psd";  
  
                break;  
  
            case "25-50-44-46":  
  
                output = " => pdf";  
  
                break;  
  
            case "49-49-2A-00":  
  
                output = " => tif";  
  
                break;  
  
            case "4D-4D-00-2A":  
  
                output = " => tif";  
  
                break;  
  
            case "null":  
  
                output = "file type is not matches with array";  
  
                break;  
  
        }  
  
        ViewBag.Message = data_as_hex;  
  
        ViewBag.signature = my;  
  
        ViewBag.out_put = output;  
  
    }  
  
    return View();  
  
}  

پروژه را اجرا نمایید

 

فایل های ضمیمه

برنامه نویسان

نویسنده 3355 مقاله در برنامه نویسان

کاربرانی که از نویسنده این مقاله تشکر کرده اند

در صورتی که در رابطه با این مقاله سوالی دارید، در تاپیک های انجمن مطرح کنید