2015-10-19 102 views
0

我遵循這個問題的解決方案Preview an image before it is uploaded顯示用戶想要提交到我的網站的圖像預覽。將瀏覽器內存中的圖像保存到服務器中

我有這樣的代碼,顯示

頁上的圖像預覽
<input type="file" accept="image/*" onchange="loadFile(event)"> 
<img id="output"/> 
<script> 
    var loadFile = function(event) { 
    var output = document.getElementById('output'); 
    output.src = URL.createObjectURL(event.target.files[0]); 
    }; 
</script> 

,並與一些額外的數據形成。

我的問題是,當用戶點擊提交表單按鈕時,如何將圖像保存到我的webapp的/ uploads /文件夾?我不需要將圖像保存在數據庫中,而是保存在文件夾中的Web服務器上。

+0

[上傳文件,ASP.NET MVC(http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx/) –

回答

0

在您的控制器的POST操作中,您可以訪問Request對象中的InputStream,然後將張貼的文件的InputStream複製到文件流中。這是一個示例代碼。

foreach (string item in Request.Files) 
{ 
    HttpPostedFileBase file = Request.Files[item]; 
    string imagePath = Path.Combine(Server.MapPath("~/Uploads"), file.FileName); 
    using(FileStream fs = File.Create(imagePath) 
    { 
     file.InputStream.CopyTo(fs); 
    } 
} 
相關問題