Remove image from content after failed upload - tinymce

Using TinyMCE 4 and the paste plugin with a custom image upload handler (based on documentation sample), I've got the upload working just fine. However, when the upload fails, the image is still added to the content. The sample indicates calling the failure method for errors but that doesn't remove the image.
I've tried adding a paste_postprocess callback to filter the content, but at that point there is no difference in the content between a successfully uploaded image and a failed one. They both appear in the content like:
<div style="display:none"><img src="data:image/jpeg;base64, datahere" /></div>
The end result in the content is actually different. A successful upload has an image source like:
<img src="http://website/uploads/mceclip11.jpg" />
Whereas a failed upload looks like:
<img src="blob:http://website/dd3bdcda-b7b1-40fe-9aeb-4214a86a92a9">
To try this out, I created a TinyMCE Fiddle here.
Any ideas on how to remove the failed upload image from the content before it's displayed to the user?

For anyone who might try something similar, I figured out a way to deal with this.
After calling the failure method as shown in the examples, I now call a method to remove the failed image before it shows up in the editor.
The function looks something like this:
function removeFailedUpload() {
var editor = tinymce.EditorManager.get('editorId');
var $html = $('<div />',{html:editor.getContent()});
$html.find('img[src^="data:"]').remove();
editor.setContent($html.html());
}

The simplest solution is to make use of the undo manager:
tinymce.activeEditor.undoManager.undo();

In version 5 you can pass an optional object to remove the image in case of failure.
function images_upload_handler(blobInfo, success, failure) {
failure('Fail fail fail', { remove: true });
alert('failed upload');
return;
},
see the docs.

I suggest two methods for more compact removal with keeping the cursor position.
Method 1:
var editor = tinymce.activeEditor;
editor.selection.collapse();
$(editor.dom.doc).find('img[src^="blob:"]').remove();
Method 2:
var editor = tinymce.activeEditor;
var img = $(editor.dom.doc).find('img[src^="blob:"]').get(0);
if (img)
editor.execCommand('mceRemoveNode', false, img);

Related

GMap.Net .ToImage() Function Outputting Images With Blank Tiles

The .ToImage() function in GMap.Net does not appear to wait for the map data to be downloaded before the screenshot is taken. This results in images with blank tiles. Is there any existing capability that would allow GMaps to wait until the map is loaded?
I already posted my question on GitHub but haven't gotten a response so far. So any help is appreciated.
VB.NET Code
Private Function TakeBitmapScreenShot() As Image
'''''''''''''''''''''''''''''''''''''''''
'Code to wait until all tiles have loaded
'''''''''''''''''''''''''''''''''''''''''
Return GMapControl.ToImage()
End Function
You can Handle the event (OnTileLoadComplete ) which fires when the tile images completly loaded and then you can take the screenshot
GMapControl1.OnTileLoadComplete += GMapControl1_OnTileLoadComplete;
private void GMapControl1_OnTileLoadComplete(long ElapsedMilliseconds)
{
GMapControl.ToImage();
}
Two of the ways around this are using the TilePrefetcher or even better this code.

Ink file picker callback called too early. How to detect when the file is available?

When uploading a file using filepicker.io, the filepicker.pick success callback is getting called before the file is actually available. Here's the code:
filepicker.pick({
mimetypes: ['image/*'],
container: 'modal',
services:['COMPUTER', 'FACEBOOK', 'INSTAGRAM', 'WEBCAM']
},
function(inkBlob){
$('img.foo').attr('src', inkBlob.url);
},
function(FPError){
console.log(FPError.toString());
});
I get a url in the inkBlob that comes in the callback, but sometimes if I insert that url into the dom (as above), I get a 404. Other times it works. I'm looking for a reliable way to know when I can use the file returned by filepicker. I figured the success callback was it, but there seems to be this race condition.
I realize I could wrap the success callback in a setTimeout, but that seems messy, and I'd like to not keep the user waiting if the file is actually available.
You can also use an event listener.
I have an ajax call that downloads an image after it's cropped by Ink. This call was failing sporadically. I fixed it by doing roughly the following:
filepicker.convert(myBlob,
{
crop: cropDimensions
},
function(croppedBlob) {
function downloadImage() {
...
}
var imageObj = new Image();
imageObj.onLoad(downloadImage()); //only download when image is there
imageObj.src = croppedBlob.url;
}
);
I have the same issue as you. My workaround was to attach an onError event to the image and have it retry on a 404 (can set a limit of retries to avoid infinite loop), but it's quite ugly and messy, so it would be great if someone came around with a better solution.

jQuery selector issue, not replacing HTML

$.ajax({
type: 'POST',
url: "ajaxClients.php",
data: '&m=removeAlert&id='+ alertId,
success: function(resultData) {
if ($('#noteRow_'+ alertId).length)
{
alert('ROW FOUND - CONTENT IS: '+ $('#noteRow_'+ alertId).html() +' -- REPLACING CONTENT NOW');
}
$('#noteRow_'+ alertId).html('<font color="red">- Note has been removed</font>');
}
});
So it is simple enough. On the success i do get the alert, it shows the content in the alert, etc.
Then right after when i try to set the html to something else, it does nothing. I have tried .empty(), .remove()... there are no console errors. Any ideas what i am missing?
EDIT - html...
<div id="noteRow_127"><img onclick="removeAlert('127')" style="cursor:pointer;" alt="Remove Message" title="Remove Message" src="images/notificationRemove.png" border="0"> [04/04/2013 06:26 PM] <b>Austin</b>: afvazf</div>
The "removeAlert()" is what fires the ajax call...
EDIT 2...
I guess somehow this is being put on the page 2 times. Although in the PHP file there is only one instance of the function that builds the rows, so i guess i just need to figure out wth is going on there. So for anyone else with this issue, inspect the element (with chrome or similar) and ctrl+f for it and see if it is on the page more than once!
FINAL EDIT:
Since i could not figure out how it was ending up on the page twice
$('[id="noteRow_'+ alertId +'"]').html('<font color="red">- Note has been removed</font>');
Took care of it!
http://jsbin.com/ujimiw/2/edit
It seems to work fine from my perspective. I just bound the click handler to the button and it works.

UIWebView loading a local image that gets overwritten but it doesn't refresh

My problem is this
I'm loading an html string into an UIWebView, in which i'm referencing a local image from my application's Documents folder.
<html><body><img src="image.png"/></body></html>
It loads just fine, no problems with the baseUrl or anything. The problem is when I do some stuff and generate a new image, with the same name, and save it in the same Documents folder, overwriting the old one (I delete the old one if it exists and save the new one).
Now, with the UIWebView still loaded on the screen, if I do a [webview reload] or manually load the html string again, I still get the old image, the one I have just overwritten.
I already checked in the simulator folder, the image.png there is indeed the new image, not the old one. And I already did everything imaginable to stop the webview from caching.
Of course, if I save the new image with a different name, say "image2.png", and reload the html string with src="image2.png", it all shows up ok.
What am I missing here?
Try appending a random query string to your image source.
ie.
function ImageSource(src){
return src + "?time=" + (new Date()).getTime().toString();
}
function ForceImageReloads(){
var images = document.getElementsByTagName("img");
for (var i=0;i<images.length;i++){
var img = images[i];
var s = img.getAttribute("src");
img.src = ImageSource(s);
}
}
ForceImageReloads();
Or something similar to that :)
This maybe a problem with the simulator. I had something similar once. Try running your code on a real device. For me, the simulator seem like it is lazy to reload data that have the same name.

SWFUpload - How to Cancel Queued Files

I'm a little confused on how to cancel an upload in SWFUpload. I know you have to pass the cancelUpload() function the ID of the upload, but it seems like when I do this it doesn't work. A sample of my code would be:
function remove(number, id) {
cancelUpload(id);
}
<span onClick = "remove(0, 'SWFUpload_0_0')">filename</span>
However, the file still uploads. Any ideas?
Have to add the variable you assign SWFUpload to. In this case, swfu.cancelUpload() worked.
It is already in handlers.js file. This will cancel your current queued file.
ar progress = new FileProgress(file, this.customSettings.progressTarget);
progress.toggleCancel(true);