जवाबों:
HTML अपलोड फ़ाइल ASP MVC 3।
मॉडल : ( ध्यान दें कि FileExtensionsAttribute MvcFutures में उपलब्ध है। यह फ़ाइल एक्सटेंशन क्लाइंट और सर्वर साइड को मान्य करेगा। )
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);
}
}
}
आप भी उपयोग कर सकते हैं:
@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>
}
कुछ समय पहले मेरा भी यही सवाल था और स्कॉट हैन्समैन के पोस्ट में से एक आया:
ASP.NET MVC के साथ टेस्ट और मोक्स सहित HTTP फ़ाइल अपलोड को लागू करना
उम्मीद है की यह मदद करेगा।
या आप इसे ठीक से कर सकते हैं:
अपने 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));
मॉडल के लिए एक अद्वितीय आईडी बनाता है, आप सूचियों और सामान में जानते हैं। मॉडल [को ०] ।नाम आदि।
मॉडल में सही संपत्ति बनाएँ:
public HttpPostedFileBase NewFile { get; set; }
फिर आपको यह सुनिश्चित करने की आवश्यकता है कि आपका फॉर्म फाइलें भेजेगा:
@using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { enctype = "multipart/form-data" }))
फिर यहाँ आपका सहायक है:
@Html.FileFor(x => x.NewFile)
पॉलियस ज़ालियाडूनिस के उत्तर का उन्नत संस्करण:
सत्यापन कार्य को ठीक से करने के लिए मुझे मॉडल को इसमें बदलना पड़ा:
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 Sagan ने केवल स्ट्रिंग के साथ काम करने वाले FileExtension विशेषता के बारे में लिखा था।
उपयोग करने के लिए BeginForm, यहाँ इसका उपयोग करने का तरीका दिया गया है:
using(Html.BeginForm("uploadfiles",
"home", FormMethod.POST, new Dictionary<string, object>(){{"type", "file"}})
यह भी काम करता है:
नमूना:
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;
}
यह मेरे हिसाब से थोड़ा सा हैकी है, लेकिन इसके परिणामस्वरूप सही सत्यापन विशेषताओं आदि को लागू किया जाता है
@Html.Raw(Html.TextBoxFor(m => m.File).ToHtmlString().Replace("type=\"text\"", "type=\"file\""))
<input type="file" />, केवल एक पाठ बॉक्स