Reputation: 578
I'm developing a multilanguaged comics website and all the inserted comics must be in english and portuguese.
I've got success on managing multiple titles doing so:
ComicViewModel.cs:
public class ComicViewModel
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage="A data não pode ficar em branco.")]
[DisplayName("Data")]
public DateTime Date { get; set; }
public IList<LocalizedTextViewModel> Titles { get; set; }
}
LocalizedTextViewModel.cs:
public class LocalizedTextViewModel
{
public CultureViewModel Culture { get; set; }
[Required(ErrorMessage = "Este campo não pode ficar em branco.")]
public string Text { get; set; }
}
CultureViewModel.cs:
public class CultureViewModel
{
public int Id { get; set; }
public string Abbreviation { get; set; }
public string Name { get; set; }
public CultureViewModel() { }
public CultureViewModel(Database.Culture culture)
{
Id = culture.Id;
Abbreviation = culture.Abbreviation;
Name = culture.Name;
}
}
The problem is I can't figure out how to manage the comic images upload. I need upload more than one image, each one referenced to it's language.
Anyone has any ideas?
Upvotes: 2
Views: 4264
Reputation: 2417
Here's an example for uploading multiple files:
The Html:
<% using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%><br />
<input type="file" name="files" id="file1" size="25" />
<input type="file" name="files" id="file2" size="25" />
<input type="submit" value="Upload file" />
<% } %>
The Controller :
[HttpPost]
public ActionResult Upload()
{
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads")
, Path.GetFileName(file.FileName));
file.SaveAs(filePath);
}
}
return RedirectToAction("Index");
}
Update : Getting some information about the uploaded file
The following example shows how you can get the name/type/size/extension of a submitted HttpPostedFileBase file.
string filename = Path.GetFileName(file.FileName);
string type = file.ContentType;
string extension = Path.GetExtension(file.FileName).ToLower();
float sizeInKB = ((float)file.ContentLength) / 1024;
suppose that you uploaded the file somePicture.jpeg
the output will be.
filename > somePicture.jpeg
type > image/jpeg
extension > jpeg
sizeInKB > // the file size.
Upvotes: 1