0

は、私は私のコントローラのコード単一のイメージを保存し、ユーザーがdropzone.jsを使用して複数のイメージを置くのを防ぐ方法はありますか?私は

[HttpGet] 
public ActionResult Show(int? id) 
{ 
    string mime; 
    byte[] bytes = LoadImage(id.Value, out mime); 
    return File(bytes, mime); 
} 

[HttpPost] 
public ActionResult Upload() 
{ 
    SuccessModel viewModel = new SuccessModel(); 
    if (Request.Files.Count == 1) 
    { 
     var name = Request.Files[0].FileName; 
     var size = Request.Files[0].ContentLength; 
     var type = Request.Files[0].ContentType; 
     viewModel.Success = HandleUpload(Request.Files[0].InputStream, name, size, type); 
    } 
    return Json(viewModel); 
} 

private bool HandleUpload(Stream fileStream, string name, int size, string type) 
{ 
    bool handled = false; 
    try 
    { 
     byte[] documentBytes = new byte[fileStream.Length]; 
     fileStream.Read(documentBytes, 0, documentBytes.Length); 
     Pictures databaseDocument = new Pictures 
     { 
      ProfilePicture=documentBytes, 
      FName=name, 
      Size=size, 
      Type=type 
     }; 
     using(var contxt=new EnglisCenterEntities()) 
     { 
      contxt.Pictures.Add(databaseDocument); 
      handled = (contxt.SaveChanges() > 0); 
     } 

    } 
    catch (Exception) 
    { 
     // Oops, something went wrong, handle the exception 
    } 
    return handled; 
} 

private byte[] LoadImage(int id, out string type) 
{ 
    byte[] fileBytes = null; 
    string fileType = null; 
    using(var contxt=new EnglisCenterEntities()) 
    { 
     var databaseDocument = contxt.Pictures.FirstOrDefault(doc => doc.IdPicture == id); 
     if (databaseDocument != null) 
     { 
      fileBytes = databaseDocument.ProfilePicture; 
      fileType = databaseDocument.Type; 
     } 

    } 
    type = fileType; 
    return fileBytes; 
} 

だdrobzone.js を使用しているデータベースに画像をアップロードしようと、これは私のスクリプト

<script type="text/javascript"> 

    $(document).ready(function() { 

    $("#preview").fadeOut(15); 
    $("#refreshButton").click(function() { 
     var imageToLoad = $("#imageId").val(); 
     if (imageToLoad.length > 0) { 
      $("#preview").attr("src", "/Document/Show/" + imageToLoad); 
      $("#preview").fadeIn(); 

     } 
    }); 
}); 

であり、これはあります私の見解

<form action="/Document/Upload" class="dropzone" id="my-awesome-dropzone"></form> 
<input type="text" name="imageId" id="imageId" /> 
<button type="button" id="refreshButton">Update Image</button> 
<img src="/" style="display: none" id="preview" /> 

それはm私は単一のイメージを保存し、ユーザーが複数のイメージを置くのを防ぎたい。単一のイメージを保存し、ユーザーがdropzone.jsを使用してイメージ以上を置くのを防ぐ方法はありますか?

答えて

1

のJavascriptは、例えばhttp://www.dropzonejs.com/#configuration-optionshttp://jsfiddle.net/P2dTF/2/を参照してください、MAXFILESを制限するために必要とされる:file

Dropzone.autoDiscover = true; 
Dropzone.options.my-awesome-dropzone = { 
    maxFiles: 1 
}; 

[HttpPost] 
public ActionResult Upload(HttpPostedFileBase file) 
{ 
    SuccessModel viewModel = new SuccessModel(); 
    if (file != null) 
    { 
     viewModel.Success = HandleUpload(file); 
    } 
    return Json(viewModel); 
} 

Paramの名は重要であり、ドロップゾーンは、(ファイルのPARAMアレイにし、複数)のparamファイルに単一のアップロードをバインドします。しかし、fileStreamが必要な理由がわかりません。部分的にダウンロードするためにリクエストヘッダー(オーディオ)などのバイト範囲を返す場合は、HttpPostedFileBaseが必要です。

private bool HandleUpload(HttpPostedFileBase file) 
{ 
    bool handled = false; 
    try 
    { 
     byte[] documentBytes = new byte[file.ContentLength]; 

     Pictures databaseDocument = new Pictures 
     { 
      ProfilePicture=documentBytes, 
      FName=file.FileName, 
      Size=file.ContentLength, 
      Type=file.ContentType 
     }; 
     using(var contxt=new EnglisCenterEntities()) 
     { 
      contxt.Pictures.Add(databaseDocument); 
      handled = (contxt.SaveChanges() > 0); 
     } 

    } 
    catch (Exception) 
    { 
     // Oops, something went wrong, handle the exception 
    } 
    return handled; 
} 
関連する問題