0

, benim denetleyicisi koduTek bir resmi kaydetmenin ve kullanıcının dropzone.js'yi kullanarak bir görüntüden daha fazlasını yapmasını engellemenin bir yolu var mı? Ben

[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; 
} 

var drobzone.js kullanıyorum veritabanında resim yüklemek için denemek ve bu benim komut

<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(); 

     } 
    }); 
}); 

ve bu benim görünümüm

ve m ile çalışıyor ulti görüntüleri ama ben tek bir görüntü kaydetmek ve kullanıcı birden fazla resim koymak önlemek istiyorum. Tek bir resmi kaydetmenin ve kullanıcının dropzone.js'yi kullanarak bir görüntüden daha fazlasını yapmasını engellemenin bir yolu var mı?

cevap

1

JavaScript, maxFiles sınırlamak örneğin http://www.dropzonejs.com/#configuration-options ve http://jsfiddle.net/P2dTF/2/ görmeyi gereklidir: file ait

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 adı önemlidir, dropzone (dosyaların bir param dizisine ve çoklu) param dosyasına tek yükleme bağlar. fileStream niçin ihtiyaç duyduğunuzu görmüyorsanız, örneğin bayt aralığını döndürmek istediğinizde fileStream gereklidir, örneğin kısmi indirme için İstek Başlığı (ses) ile HttpPostedFileBase işinizdeki işi yapar.

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; 
} 
İlgili konular