2010-09-13 56 views
2

我正在開發一個多語言的漫畫網站,所有插入的漫畫必須使用英語和葡萄牙語。如何上傳ASP.NET MVC2上的文件?

我已經有了管理多個頭銜成功這樣做:

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; 
    } 
} 

問題是我無法弄清楚如何管理漫畫圖片上傳。我需要上傳多個圖片,每個圖片都引用它的語言。

任何人有任何想法?

+0

只見[這裏](http://bartwullems.blogspot.com/2010/01/uploading-files-using-aspnet-mvc-2.html)的方式來處理上傳的文件。但使用'IEnnumerable'我無法知道每種文件的語言。 – 2010-09-13 13:26:17

回答

1

下面是上傳多個文件的例子:

的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" />  
<% } %> 

控制器:

[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"); 
} 

更新:獲取有關上傳文件

一些信息

以下例子mple顯示瞭如何獲取提交的HttpPostedFileBase文件的名稱/類型/大小/擴展名。

string filename = Path.GetFileName(file.FileName); 

string type = file.ContentType; 

string extension = Path.GetExtension(file.FileName).ToLower(); 

float sizeInKB = ((float)file.ContentLength)/1024; 

假設您上傳了文件somePicture.jpeg的輸出結果。

filename > somePicture.jpeg 
type  > image/jpeg 
extension > jpeg 
sizeInKB > // the file size. 
+0

好吧,它獲取的圖像,但我需要知道哪種文化是每個圖像。我該如何重構?我可以更改表單上的名稱屬性嗎? – 2010-09-13 15:30:38

+0

@Rodrigo Waltenberg:我希望我的更新能夠解答您的第一個問題,關於名稱屬性,您可以將其更改爲任何您想要的。 – 2010-09-13 17:52:55

+0

你的答案解決了我的問題。但我認爲我沒有很好地表達自己......我需要它在ViewModel中,所以這樣做對我來說不起作用。我會將這個問題標記爲已解決,並在另一個線程中重建我的問題。不管怎麼說,還是要謝謝你 – 2010-09-13 20:42:26