有没有一种方法可以保存单个图像,并防止用户使用dropzone.js放置多个图像
Is there a way to save a single image and to prevent user put more than an image using dropzone.js?
我正在尝试在数据库中上传图像,我使用的是drobzone.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;
}
这是我的脚本
<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" />
它可以处理多个图像,但我想保存单个图像,并防止用户放置多个图像。有没有一种方法可以保存单个图像,并防止用户使用dropzone.js放置多个图像?
需要Javascript来限制maxFiles,请参阅http://www.dropzonejs.com/#configuration-选项和http://jsfiddle.net/P2dTF/2/例如:
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);
}
file
的Param名称很重要,dropzone将单个上传绑定到Param文件(将多个上传绑定到文件的Param数组)。不过,不明白为什么需要fileStream
,当你想返回一系列字节时需要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;
}
相关文章:
- 是否可以仅通过jQuery将图像替换为用户动态选择的另一个图像
- 如何在用户单击时创建带有弹出窗格的图像
- 如果图像标记是用javascript编写的,并且需要用户输入,那么图像不会随页面加载吗
- 有人知道有没有jquery插件可以在图像上写文本,并让用户将其放置在图像内的任何位置
- 如何在windows 8 html5现代用户界面中显示没有背景的图像
- 带图像的角度用户界面选择
- HTML - 如何在不同的用户代理中显示不同的图像
- 如何以用户友好的方式在计算机上保存从画布转换的图像
- 我在Phaser有一个项目.我想在画布上的元素上做一个用户图像编辑器
- 纯javascript的进度图像加载程序仅当用户单击提交html表单时
- 添加/到URL的末尾将用户带到随机页面,并在其他页面上剥离图像
- 压缩&调整用户上传图像的大小
- 使用Cordova contacts插件将图像保存到用户的联系人中
- 让用户手动裁剪特定大小的图像作为输出
- 显示用户's在图像地图上的位置
- 如何将SVG元素和Div放入图像中,以便用户可以下载
- 有没有一种方法可以保存单个图像,并防止用户使用dropzone.js放置多个图像
- 编写替换背景图像的用户脚本
- 将图像保存到用户的pc缓存中,并使用asp.net从缓存中使用它
- 使用Facebook API将图像和评论发布到用户墙的简单方法