2014-04-02 64 views
1

我工作的一個ASP.net MVC4應用控制器,我想通過表單發送的圖片,我有我的看法控制器 這裏是我的看法通IMG在剃刀

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

          <img src="img/annonceBrute.JPG" width ="60" height="60" name ="imageFile" /> 

          @Html.TextArea("resultText") 
          <input type="submit" style="margin-left:40px;cursor:pointer;" id="l" value="Envoyer"/> 
          } 

而在我的控制器中,我有一個可以與上傳的圖像一起工作的代碼,但是我想使用一個已經存在於我的視圖中的圖像。這裏是控制器的代碼

public ActionResult Index(HttpPostedFileBase imageFile) 
    { 
     var db = new Bd_scanitEntities(); 
     IEnumerable<SelectListItem> items = db.JournalSet 
      .Select(c => new SelectListItem 
      { 
       Value = c.Id.ToString(), 
       Text = c.label 
      }); 
     ViewBag.IdJournal1 = items; 

     //Conversion 
     if (imageFile!= null && imageFile.ContentLength > 0) 
     { 

      // for now just fail hard if there's any error however in a propper app I would expect a full demo. 

      using (var engine = new TesseractEngine(Server.MapPath(@"./tessdata"), "eng", EngineMode.Default)) 
      { 
       // have to load Pix via a bitmap since Pix doesn't support loading a stream. 
       using (var image = new System.Drawing.Bitmap(imageFile.InputStream)) 
       { 
        using (var pix = PixConverter.ToPix(image)) 
        { 
         using (var page = engine.Process(pix)) 
         { 
          //meanConfidenceLabel.InnerText = String.Format("{0:P}", page.GetMeanConfidence()); 
          //ViewBag.meanConfidenceLabel = String.Format("{0:P}", page.GetMeanConfidence()); 
          ViewBag.resultText = page.GetText(); 

         } 
        } 
       } 
      } 

     } 

     return View(); 
    } 

我的問題是,我不知道我應該在索引參數,以便從該視圖獲取圖像使用哪種類型。

+0

我認爲你需要一個簡單的GET請求處理程序。 'HttpPostedFileBase'應該和POST請求處理器一起使用。 –

+0

您的服務器上有圖像嗎? –

回答

1

您不能發送圖像控制器這樣,如果你只需要在控制器的圖像的路徑,使用隱藏域:

<input type="hidden" name="image" value="img/annonceBrute.JPG"/> 
如果你希望整個圖像服務器上發佈

,你需要使用input type file你不能發佈一個html顯示標籤到服務器使用表格,只有表格輸入字段發佈在服務器上。

在控制器動作

你可以閱讀文件是這樣的:

public ActionResult MyAction(FormCollection form) 
{ 

    string filePath = Server.MapPath(form["image"].ToString());  

    byte[] buffer; //file bytes 
     FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); 
     try 
     { 
     int length = (int)fileStream.Length; // get file length 
     buffer = new byte[length];   // create buffer 
     int count;       // actual number of bytes read 
     int sum = 0;       // total number of bytes read 

     // read until Read method returns 0 (end of the stream has been reached) 
     while ((count = fileStream.Read(buffer, sum, length - sum)) > 0) 
      sum += count; // sum is a buffer offset for next reading 
     } 
     finally 
     { 
     fileStream.Close(); 
     } 
     return View(); 
} 
+0

是的。然後在你的控制器上,你可以通過'Server.MatpPath(「〜/ img/annonceBrute.JPG」)得到一個圖像的路徑'' –

+0

非常感謝你 – ItShine

1

,因爲它看起來,你想要從視圖中上傳圖片,並在控制器獲取HttpPostedFileBase所以使用輸入文件標籤

<input id="image1" name="image1" type="file" /> 

在控制器的行動,你應該得到像這樣的HttpPosted文件

if (Request.Files.Count > 0) 
       { 
        if (Request.Files["image1"].ContentLength > 0) 
        { 
         HttpPostedFileBase pf = Request.Files["image1"] 
        }      
       } 

現在喲你可以保存這個HttpPostedFileBase或什麼是你的要求