[英]Get img in MVC Controller
I currently have a working MVC solution which acts as a kind of "IT Helpdesk", I would like to add the functionality of uploading images (easily done by using <input type="file"..../>
) however as a lot of people will be copying images from the snipping tool, I am trying to allow them to paste images into the form. 我目前有一个工作的MVC解决方案,作为一种“IT帮助台”,我想添加上传图像的功能(通过使用
<input type="file"..../>
轻松完成)但是作为一个很多人会从剪切工具中复制图像,我试图让他们将图像粘贴到表单中。 This bit works as you can see in the fiddle , however i cannot work out how to then get these images in the controller, if possible at all. 这个位就像你在小提琴中看到的一样,但是我无法弄清楚如何在控制器中获取这些图像,如果可能的话。
View 视图
<div class="col-sm-12 col-md-12 col-lg-12">
<label for="fileUploads" class="col-sm-3 col-md-3 col-lg-3">File(s) Upload</label>
<div name="fileUploads" id="fileUploads" multiple contenteditable="true" style="outline: 1px solid black; overflow: auto;"></div>
</div>
JQuery JQuery的
document.getElementById("fileUploads").focus();
document.body.addEventListener("paste", function (e) {
for (var i = 0; i < e.clipboardData.items.length; i++) {
if (e.clipboardData.items[i].kind == "file" && e.clipboardData.items[i].type == "image/png") {
// get the blob
var imageFile = e.clipboardData.items[i].getAsFile();
// read the blob as a data URL
var fileReader = new FileReader();
fileReader.onloadend = function (e){
// create an image
var image = document.createElement("IMG");
image.src = this.result;
// insert the image
var range = window.getSelection().getRangeAt(0);
range.insertNode(image);
range.collapse(false);
// set the selection to after the image
var selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
};
// TODO: Error Handling!
// fileReader.onerror = ...
fileReader.readAsDataURL(imageFile);
// prevent the default paste action
e.preventDefault();
// only paste 1 image at a time
break;
}
}
});
This is where the JQuery came from 这就是JQuery的来源
The result of this JQuery inserts an <img>
inside of the <div>
eg 这个JQuery的结果在
<div>
插入了一个<img>
,例如
<div><img src="...." /> </div>
I suspect the <img>
element in the browser contains a base64 encoded image URL right? 我怀疑浏览器中的
<img>
元素是否包含base64编码的图像URL?
Why don't you take the contents of the src
attribute and post them back to the server? 为什么不取
src
属性的内容并将它们发回服务器?
Code sample to get you started (in php) here: 代码示例让你开始(在PHP中)这里:
How to save a PNG image server-side, from a base64 data string 如何从base64数据字符串保存PNG映像服务器端
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.