2008-11-20 17 views
107

क्या फ़ाइल अपलोड के लिए HTMLHelper है? विशेष रूप से, मैं<इनपुट प्रकार = "फ़ाइल के लिए एचटीएमएल सहायक" />

<input type="file"/> 

एएसपी.नेट एमवीसी HTMLHelper का उपयोग कर एक प्रतिस्थापन की तलाश में हूं।

या, अगर मैं

using (Html.BeginForm()) 

का उपयोग फ़ाइल के लिए HTML नियंत्रण क्या अपलोड किया जाता है?

उत्तर

181

HTML अपलोड फ़ाइल एएसपी MVC 3.

मॉडल : (ध्यान दें कि FileExtensionsAttribute एमवीसीफ्यूचर में उपलब्ध है। यह होगा फ़ाइल एक्सटेंशन क्लाइंट साइड और सर्वर पक्ष को मान्य करें।)

public class ViewModel 
{ 
    [Required, Microsoft.Web.Mvc.FileExtensions(Extensions = "csv", 
      ErrorMessage = "Specify a CSV file. (Comma-separated values)")] 
    public HttpPostedFileBase File { get; set; } 
} 

HTML दृश्य:

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new 
             { enctype = "multipart/form-data" })) 
{ 
    @Html.TextBoxFor(m => m.File, new { type = "file" }) 
    @Html.ValidationMessageFor(m => m.File) 
} 

नियंत्रक कार्रवाई:

[HttpPost] 
public ActionResult Action(ViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     // Use your file here 
     using (MemoryStream memoryStream = new MemoryStream()) 
     { 
      model.File.InputStream.CopyTo(memoryStream); 
     } 
    } 
} 
+0

यह फ़ाइल इनपुट '<इनपुट प्रकार = "फ़ाइल" /> 'प्रस्तुत नहीं करता है, केवल एक टेक्स्ट बॉक्स – Ben

+0

बेन, फ़ाइल इनपुट प्रस्तुत करने के लिए http://jsfiddle.net/ आज़माएं। –

+0

@PauliusZaliaduonis लाइन के साथ Microsoft.Web.Mvc.FileExtensions एमवीसी लाल के रूप में रेखांकित किया गया है। मुझसे इसका समाधान किस प्रकार होगा? – Pomster

7

मैं एक समय पहले यह एक ही सवाल था और स्कॉट Hanselman पदों में से एक में आए:

Implementing HTTP File Upload with ASP.NET MVC including Tests and Mocks

आशा इस मदद करता है।

+0

धन्यवाद, लेकिन मैं विशेष रूप से (HTML.BeginForm()) का उपयोग करने के कार्यान्वयन की तलाश कर रहा हूं, अन्य रूपों में नहीं। – Graviton

2

BeginForm का उपयोग करने के लिए, यहाँ जिस तरह से यह उपयोग करने के लिए है:

using(Html.BeginForm("uploadfiles", 
"home", FormMethod.POST, new Dictionary<string, object>(){{"type", "file"}}) 
+2

सबसे पहले आप इनपुट तत्व उत्पन्न करने का उल्लेख करते हैं, और अब आप फ़ॉर्म तत्व उत्पन्न करने के तरीके के बारे में बात करते हैं? क्या यह वास्तव में आपका जवाब है? – Pablo

+0

आप सही हैं; मैंने अभी अपना जवाब संपादित कर लिया है। – Graviton

+0

http://stackoverflow.com/questions/216600/html-beginform-and-adding-properties –

18

तुम भी उपयोग कर सकते हैं:

@using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    <p> 
     <input type="file" id="fileUpload" name="fileUpload" size="23" /> 
    </p> 
    <p> 
     <input type="submit" value="Upload file" /></p> 
} 
+1

और नियंत्रक जैसा दिखता है ....? – JsonStatham

4

पौलिअस Zaliaduonis 'जवाब के उन्नत संस्करण:

में

public class ViewModel 
{ 
     public HttpPostedFileBase File { get; set; } 

     [Required(ErrorMessage="A header image is required"), FileExtensions(ErrorMessage = "Please upload an image file.")] 
     public string FileName 
     { 
      get 
      { 
       if (File != null) 
        return File.FileName; 
       else 
        return String.Empty; 
      } 
     } 
} 

और करने के लिए दृश्य:

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new 
             { enctype = "multipart/form-data" })) 
{ 
    @Html.TextBoxFor(m => m.File, new { type = "file" }) 
    @Html.ValidationMessageFor(m => m.FileName) 
} 

इसका कारण यह है कि क्या @Serj सगन के बारे में FileExtension विशेषता केवल तार के साथ काम करने में लिखा था की आवश्यकता है क्रम मान्यता काम करने के लिए ठीक से मैं करने के लिए मॉडल बदलना पड़ा ।

+0

क्या आप इस जवाब को पॉलियस के जवाब में विलय नहीं कर सकते? – Graviton

-1

यह एक छोटे से hacky मुझे लगता है कि है, लेकिन यह सही सत्यापन में जो परिणाम विशेषताओं आदि लागू किया जा रहा

@Html.Raw(Html.TextBoxFor(m => m.File).ToHtmlString().Replace("type=\"text\"", "type=\"file\"")) 
0

यह भी काम करता है:

मॉडल:

public class ViewModel 
{   
    public HttpPostedFileBase File{ get; set; } 
} 

देखें:

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new 
             { enctype = "multipart/form-data" })) 
{ 
    @Html.TextBoxFor(m => m.File, new { type = "file" })  
} 

नियंत्रक कार्रवाई:

[HttpPost] 
public ActionResult Action(ViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     var postedFile = Request.Files["File"]; 

     // now you can get and validate the file type: 
     var isFileSupported= IsFileSupported(postedFile); 

    } 
} 

public bool IsFileSupported(HttpPostedFileBase file) 
      { 
       var isSupported = false; 

       switch (file.ContentType) 
       { 

        case ("image/gif"): 
         isSupported = true; 
         break; 

        case ("image/jpeg"): 
         isSupported = true; 
         break; 

        case ("image/png"): 
         isSupported = true; 
         break; 


        case ("audio/mp3"): 
         isSupported = true; 
         break; 

        case ("audio/wav"): 
         isSupported = true; 
         break;         
       } 

       return isSupported; 
      } 

List of contentTypes

3

या आप इसे ठीक से कर सकता है:

अपने HtmlHelper एक्सटेंशन वर्ग में:

public static MvcHtmlString FileFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) 
    { 
     return helper.FileFor(expression, null); 
    } 

public static MvcHtmlString FileFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) 
    { 
     var builder = new TagBuilder("input"); 

     var id = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); 
     builder.GenerateId(id); 
     builder.MergeAttribute("name", id); 
     builder.MergeAttribute("type", "file"); 

     builder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); 

     // Render tag 
     return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing)); 
    } 

यह पंक्ति:

var id = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression)); 

मॉडल के लिए अद्वितीय आईडी उत्पन्न करता है, जिसे आप सूचियों और सामानों में जानते हैं। मॉडल [0] .Name आदि

मॉडल में सही प्रॉपर्टी बनानी होगी:

public HttpPostedFileBase NewFile { get; set; } 

तो फिर तुम सुनिश्चित करें कि आपके प्रपत्र फ़ाइलें भेज देंगे बनाने की जरूरत है:

@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" })) 

तो यहाँ सहायक है :

@Html.FileFor(x => x.NewFile) 
संबंधित मुद्दे