2013-10-09 144 views
0

我想構建一個電子商務應用程序。我的任務是存儲用戶從他的機器上傳到用戶數據庫中的圖像,其中用戶上傳的圖像是在mvc數據庫中存儲圖像

。我爲列創建了一個blob數據類型,其中

存儲圖像。現在

查看代碼

@using (Html.BeginForm()) 
{ 
    <div class="editor-field"> 
       @Html.TextBoxFor(m => m.Description, new { id = "txtDescription" }) 
       @Html.ValidationMessageFor(m => m.Description) 
      </div> 
      <div class="editor-label"> 
       @Html.Label("Upload your picture"); 
      </div> 
      <p> 
       <input type="file" id="imagesubmit" name="imagesubmit" value="Submit" 
onclick="saveuserinfo();"/> 
      </p> 
} 

JavaScript代碼

function saveuserinfo() 
{ 
     var description = document.getElementById('txtDescription').value; 
     var imagefile = document.getElementById('imagesubmit'); 
     $.ajax({ 
      url: '@Url.Action("SaveuserInfo", "Welcome")', 
      data: { description: description,imagesubmit: imagesubmit}, 
      type: 'POST', 
      success: function (data) { 
          } 
     }); 
    } 

控制器代碼

??? 

我需要在如何從JavaScript獲取圖像,並把它傳遞給DB層控制器代碼用於插入

數據庫中的

這是我一直在努力..是這個正確的方法來存儲圖像

public ActionResult saveuserinfo(string description,) 
    { 
     return View("WelcomePage"); 
    } 

(我有我要在這裏使用收到的形象和什麼什麼數據類型的代碼被困在這裏

數據類型,我必須轉換併發送至DB層

+0

的可能重複[文件上傳ASP.NET MVC 3.0] (http://stackoverflow.com/questions/5193842/file-upload-asp-net-mvc-3-0) –

+0

保存圖像文件不是更好,然後將文件位置存儲在數據庫中嗎? –

回答

0

使用jQuery AJAX,或任何形式的普通的XMLHttpRequest的你不能上傳文件。

您應該使用

@using (Html.BeginForm("YourAction", "YourController", FormMethod.Post, new { enctype = multipart/form-data" })) { 
// your form elements goes here, e.g <input type="file" ..... 
} 

或者,如果你真的想在你上傳一個ajax的感覺,你應該看看精者:https://github.com/Widen/fine-uploader

+0

這給了我的出發點。 – user2465036

0

你需要一個MultifilePart參數添加到你的動作:

public ActionResult saveuserinfo(@RequestParam MultipartFile image) {...} 

該參數具有允許您訪問字節的方法。注意:您只能閱讀一次流!

文檔:http://docs.spring.io/spring/docs/3.2.1.RELEASE/spring-framework-reference/html/mvc.html#mvc-multipart

+0

如果我沒有弄錯,問題是ASP.NET MVC,而不是Java上的Spring。 –

+0

@JoeEnos:這是可能的。我會等待他的澄清。 –