我要做的是在Dropzone之前。js将删除的图像发送到服务器,会出现一个带有裁剪器的模式。js(fengyuanchen脚本),用户可以裁剪图像,当图像被裁剪时,用Dropzone发送。js连接到服务器。
因此,当我用函数fileToBase64更改#cropbox的图像src,并用函数croper('replace')替换裁剪器的图像时,它会一直显示默认值。jpg图片,而不是用户上传的新图片
HTML
<div class="wrapper-crop-box">
<div class="crop-box">
<img src="default.jpg" alt="Cropbox" id="cropbox">
</div>
</div>
JS:
function fileToBase64(file) {
var preview = document.querySelector('.crop-box img');
var reader = new FileReader();
reader.onloadend = function () {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
}
$(function() {
Dropzone.options.avtDropzone = {
acceptedFiles: 'image/*',
autoProcessQueue: true,
paramName: 'file',
maxFilesize: 2,
maxFiles: 1,
thumbnailWidth: 200,
thumbnailHeight: 200,
accept: function(file, done) {
fileToBase64(file);
$('#cropbox').cropper('replace', $('#cropbox').attr('src'));
$('.wrapper-crop-box').fadeIn();
done();
},
init: function() {
this.on("addedfile", function(file) {
if (this.files[1]!=null){
this.removeFile(this.files[0]);
}
});
}
};
$('#cropbox').cropper({
aspectRatio: 1 / 1,
resizable: false,
guides: false,
dragCrop: false,
autoCropArea: 0.4,
checkImageOrigin: false,
preview: '.avatar'
});
});
以下是提供此功能的要点。
https://gist.github.com/maria-p/8633b51f629ea8dbd27e
// transform cropper dataURI output to a Blob which Dropzone accepts
function dataURItoBlob(dataURI) {
var byteString = atob(dataURI.split(',')[1]);
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ab], { type: 'image/jpeg' });
}
// modal window template
var modalTemplate = '<div class="modal"><!-- bootstrap modal here --></div>';
// initialize dropzone
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone(
"#my-dropzone-container",
{
autoProcessQueue: false,
// ..your other parameters..
}
);
// listen to thumbnail event
myDropzone.on('thumbnail', function (file) {
// ignore files which were already cropped and re-rendered
// to prevent infinite loop
if (file.cropped) {
return;
}
if (file.width < 800) {
// validate width to prevent too small files to be uploaded
// .. add some error message here
return;
}
// cache filename to re-assign it to cropped file
var cachedFilename = file.name;
// remove not cropped file from dropzone (we will replace it later)
myDropzone.removeFile(file);
// dynamically create modals to allow multiple files processing
var $cropperModal = $(modalTemplate);
// 'Crop and Upload' button in a modal
var $uploadCrop = $cropperModal.find('.crop-upload');
var $img = $('<img />');
// initialize FileReader which reads uploaded file
var reader = new FileReader();
reader.onloadend = function () {
// add uploaded and read image to modal
$cropperModal.find('.image-container').html($img);
$img.attr('src', reader.result);
// initialize cropper for uploaded image
$img.cropper({
aspectRatio: 16 / 9,
autoCropArea: 1,
movable: false,
cropBoxResizable: true,
minContainerWidth: 850
});
};
// read uploaded file (triggers code above)
reader.readAsDataURL(file);
$cropperModal.modal('show');
// listener for 'Crop and Upload' button in modal
$uploadCrop.on('click', function() {
// get cropped image data
var blob = $img.cropper('getCroppedCanvas').toDataURL();
// transform it to Blob object
var newFile = dataURItoBlob(blob);
// set 'cropped to true' (so that we don't get to that listener again)
newFile.cropped = true;
// assign original filename
newFile.name = cachedFilename;
// add cropped file to dropzone
myDropzone.addFile(newFile);
// upload cropped file with dropzone
myDropzone.processQueue();
$cropperModal.modal('hide');
});
});
请注意,我不是作者。
对于不希望或不能拥有jQuery依赖项的开发人员。
我最近写了一篇关于Dropzone与Cropper集成的文章。js,这可能有用。请参阅下面文章中的代码片段。
超文本标记语言
<div class="dropzone" id="myDropzone"></div>
JavaScript
Dropzone.options.myDropzone = {
url: '/post',
transformFile: function(file, done) {
var myDropZone = this;
// Create the image editor overlay
var editor = document.createElement('div');
editor.style.position = 'fixed';
editor.style.left = 0;
editor.style.right = 0;
editor.style.top = 0;
editor.style.bottom = 0;
editor.style.zIndex = 9999;
editor.style.backgroundColor = '#000';
// Create the confirm button
var confirm = document.createElement('button');
confirm.style.position = 'absolute';
confirm.style.left = '10px';
confirm.style.top = '10px';
confirm.style.zIndex = 9999;
confirm.textContent = 'Confirm';
confirm.addEventListener('click', function() {
// Get the canvas with image data from Cropper.js
var canvas = cropper.getCroppedCanvas({
width: 256,
height: 256
});
// Turn the canvas into a Blob (file object without a name)
canvas.toBlob(function(blob) {
// Update the image thumbnail with the new image data
myDropZone.createThumbnail(
blob,
myDropZone.options.thumbnailWidth,
myDropZone.options.thumbnailHeight,
myDropZone.options.thumbnailMethod,
false,
function(dataURL) {
// Update the Dropzone file thumbnail
myDropZone.emit('thumbnail', file, dataURL);
// Return modified file to dropzone
done(blob);
}
);
});
// Remove the editor from view
editor.parentNode.removeChild(editor);
});
editor.appendChild(confirm);
// Load the image
var image = new Image();
image.src = URL.createObjectURL(file);
editor.appendChild(image);
// Append the editor to the page
document.body.appendChild(editor);
// Create Cropper.js and pass image
var cropper = new Cropper(image, {
aspectRatio: 1
});
}
};
集成代码的演示可以在这里找到:https://codepen.io/rikschennink/pen/PXQNGp?editors=0010
你可能不再需要它了,但我会把它留在这里:)
这有点棘手,我的解决方案可能有点“黑客化”,但它是有效的,你不必在服务器上上传文件来调整大小。
我也在模态窗口中使用cropper。我想强制用户在上传到服务器之前将图像裁剪到特定的维度。
确认模式图像中的裁剪后,立即上传。
// transform cropper dataURI output to a Blob which Dropzone accepts
function dataURItoBlob(dataURI) {
var byteString = atob(dataURI.split(',')[1]);
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ab], { type: 'image/jpeg' });
}
// modal window template
var modalTemplate = '<div class="modal"><!-- bootstrap modal here --></div>';
// initialize dropzone
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone(
"#my-dropzone-container",
{
autoProcessQueue: false,
// ..your other parameters..
}
);
// listen to thumbnail event
myDropzone.on('thumbnail', function (file) {
// ignore files which were already cropped and re-rendered
// to prevent infinite loop
if (file.cropped) {
return;
}
if (file.width < 800) {
// validate width to prevent too small files to be uploaded
// .. add some error message here
return;
}
// cache filename to re-assign it to cropped file
var cachedFilename = file.name;
// remove not cropped file from dropzone (we will replace it later)
myDropzone.removeFile(file);
// dynamically create modals to allow multiple files processing
var $cropperModal = $(modalTemplate);
// 'Crop and Upload' button in a modal
var $uploadCrop = $cropperModal.find('.crop-upload');
var $img = $('<img />');
// initialize FileReader which reads uploaded file
var reader = new FileReader();
reader.onloadend = function () {
// add uploaded and read image to modal
$cropperModal.find('.image-container').html($img);
$img.attr('src', reader.result);
// initialize cropper for uploaded image
$img.cropper({
aspectRatio: 16 / 9,
autoCropArea: 1,
movable: false,
cropBoxResizable: true,
minContainerWidth: 850
});
};
// read uploaded file (triggers code above)
reader.readAsDataURL(file);
$cropperModal.modal('show');
// listener for 'Crop and Upload' button in modal
$uploadCrop.on('click', function() {
// get cropped image data
var blob = $img.cropper('getCroppedCanvas').toDataURL();
// transform it to Blob object
var newFile = dataURItoBlob(blob);
// set 'cropped to true' (so that we don't get to that listener again)
newFile.cropped = true;
// assign original filename
newFile.name = cachedFilename;
// add cropped file to dropzone
myDropzone.addFile(newFile);
// upload cropped file with dropzone
myDropzone.processQueue();
$cropperModal.modal('hide');
});
});
我必须向服务器发送一个映像(我认为最好的选择是使用HttpURLConnection)并从它接收一个字符串答案。在我读过的不同的文档和web站点中,我研究了这样做的最佳方法是使用多伙伴关系。 1_做它是最好的解决方案吗? 2_我有一个错误,说Android Studio无法解析符号'multipartentity'。我读到,要解决它,我必须下载外部库。哪些是它们,我如何下载它们? 3_为此,我想在
问题内容: 我要尝试做的就是从服务器中搜索文件并显示图片。HTML有一个简单的搜索栏,您可以输入搜索词。JavaScript使用ajax请求来调用PHP文件,然后PHP在服务器上找到图像并将其发送回以显示。 现在发生的是该图像没有显示,并且我得到一个图标,指示一些无效的图像。Ajax调用似乎正在运行,但是我认为我发回的数据不正确。我一直在尝试搜索它,但是每个人似乎对如何执行它都有不同的看法,这有点
问题内容: 我要上传图像并将其保存在服务器中。我上传了图像并获得了预览,但是我被困在将图像发送到服务器上。我想使用角度服务将此图像发送到服务器。 这是HTML代码 这是指令 问题答案: 假设您在后端中期望Multipart,这是一段对我有用的代码。 这是一个jsfiddle。 请注意 以下部分: 是一些Angular魔术,为了使$ http解析FormData并找到正确的内容类型,等等。
我正在尝试将发送到我的 Rails 服务器。但是,当我尝试构建它时,它会崩溃并给我错误 03-25 09:44:50.001 W/system . err£Java . util . concurrent . execution exception:Java . lang . nosuchmethod error:没有静态方法create(Ljava/lang/String;[Lorg/Apach
我应该如何实现改造到这个代码发送图像到服务器,而不是使用Volley?我有点困惑,因为我对Java有点陌生 /Android.我想知道一些关于如何使用改造实现这一点的指导,因为Volley似乎不工作。 我基本上是向我的服务器发送一个base64字符串的图像,以及图像名称和其他一些内容。我还需要从我的服务器检索响应并将其显示在我的应用程序上。凌空截击似乎很容易做到这一点,但对改装并不确定。 提前谢谢
在我的代码中,我通过java套接字将图像从PC传输到手机。一切正常,图片保存到手机上,没有问题。但问题出现了,有没有可能在屏幕上显示图像而不保存? 服务器代码: 客户端代码: