2014-04-02 105 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或什么是你的要求