Remove Image from its folder using javascript with Asp.Net - javascript

I am trying to delete an Image which are generated dynamically using javascript with Asp.Net and I am only able to delete it from the div container and from not is original path.
So how do I do that?
Here is my code:
$('#container').append("<div class='container a'><a href='#'><img src='uploads/" + document.getElementById("currentDirectory").value + "/" + file.name + "' width='64' height='64'/><span></span></a></div>");
$('.container a span').live('click', function (e) {
$(this).closest('div.container').fadeOut("normal", function () {
var ImagePath ="uploads/" + document.getElementById("currentDirectory").value + "/" + file.name + "";
var sPath = Server.MapPath(ImagePath) + dataName + fileName;
$(sPath).remove();
});
return false;
});

You won't be able to directly remove an image from the server using javascript alone. The only way you can achieve this is to call a server side method to do this for you.
In terms of .NET you can achieve this by writing a method on your server and exposing it as a web service. You can then call this method using jQuery's ajax functionality passing it the image name to remove. I would suggest restricting your web service to ajax POST requests and, as Richard pointed out in the comments, resticting what can and can't be deleted.

Related

Javascript Link to Subdomains

I am developing an ASP.NET MVC website.
The dev website is on the root , so I can access it directly at http://localhost.
In the production machine, it is not on the root, so it should be accessed by: http://server/mywebsite.
In the view, I have a link to edit the object by calling an action method on the controller so that I can call it by: /School/Edit/1
however on the production, this will not work, I need to call it as: /mywebsite/School/Edit/1
In order to solve this, I did the following in the Javascript file:
var APP_PATH = "/mywebsite/";
function getEditLink(controller,action, id) {
return "<a href='" + APP_PATH + controller+"/"+ action + "/" + id + "'>" +"Edit"+"</a>";
}
on the development, I set the APP_PATH to an empty string, when I need to upload to the production I set it to "/mywebsite/".
I know there should be some mapping to do this instead of doing it like what I did, something like the Server.MapPath("~") in ASP.Net.

How do I add the parameter of a javascript function into a url?

Say I have a function:
function potato(param) {
document.body.style.backgroundImage = 'url(file:///C:/Users/Joe)';}
and I want to make it so when I call potato('/chicken.jpg'), it will modify the url in potato to be file:///C:/Users/Joe/chicken.jpg. How could this be done? Thank you
Javascript in a browser doesn't have access to the file system. Try running a local web browser with a document root of C:\User\Joe and then pass in the url path ... something like 'http://localhost/chicken.jpg'
Just use concatenate operator. So in JavaScript use +.
'my/base/url' + myStringVar;
'url(file:///C:/Users/Joe'+param+')';
I think you want something like this. This code will replace the background image when you call the function with the param :
function potato(param){
var $element = document.querySelector('.something'),
url = 'file:///C:/Users/Joe/' + param;
$element.style.backgroundImage = "url('" + url + "')";
console.log('=>' + $element.style.backgroundImage);
}
This is live example on jsbin => https://jsbin.com/?html,console,output

Google Apps Script, function to return text as hyperlink

I have a Google Apps Script function SearchFiles() which returns the download urls depending on the keyword the user provides. It is something like this:
function SearchFiles(inputValueFromUser) {
var searchFor ='title contains "' + inputValueFromUser + '"';
var searchForAbsString = inputValueFromUser + '.pdf' ;
//The Rest of the Code goes here
if(file.getName() == searchForAbsString){
downloadURL = "https://drive.google.com/uc?export=download&id=" + fileId;
arryOfUrls.push(downloadURL);
};
};
Logger.log('arryOfUrls: ' + arryOfUrls);
return arryOfUrls;//.toString(); //Send array of download urls back to client side JavaScript
};
The function is returning arryOfUrls in plain text.
1) How can it be sent in hyperlink form?
2) Is it possible to open the URL automatically(start download immediately) as one clicks the respective button( on Index.html) to get the link?
No it can't be sent as a hyperlink directly (at least not as far I know). But you can have the HTML information embedded into the string (like <a href=downloadUrl>...</a>"). This can be interpreted on the client side as a link.

How to make these links to download files when clicked in node.js?

I'm new to Node and i'm really stuck.
I'm trying to make my web server to look up files in current directory and to display them in browser as links so i could download them.
However the list of files gets just updated with every request and displayed over and over. I can't use any frameworks or external components and i have been stuck on this for two days. I really have done a lot of research and tried a lot of things and still can't get it working.
I'm gonna add my last attempt's code below and if anyone could help me with even a little bit of information, it would be most appreciated. Thanks!
var http = require("http");
var fs = require("fs");
var currentServerDir = ".";
var port = 8080;
var content = "";
var server = http.createServer(handlerRequest).listen(8080);
function handlerRequest(request, response) {
fs.readdir(currentServerDir, function getFiles(error, items) {
items.forEach(function getItems(item) {
content += "<br>" + item + "<br>";
});
});
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.write(content);
response.end();
}
EDIT:
I followed Node.js Generate html
and borrowed some code from there. Now i can click on a file, but instead of downloading or viewing it just says "undefined".
var http = require('http');
var content
function getFiles()
{
fs.readdir(currentServerDir, function getFiles(error, items)
{
items.forEach(function getItems(item)
{
content+= "<br>" +item + "<br>";
});
});
}
http.createServer(function (req, res) {
var html = buildHtml(req);
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': html.length,
'Expires': new Date().toUTCString()
});
res.end(html);
}).listen(8080);
function buildHtml(req) {
var header = '';
var body = content;
return '<!DOCTYPE html>'
+ '<html><header>' + header + '</header><body>' + body + '</body> </html>';
};
There are a few issues in your code.
You never call getFiles() resulting that content will never be filled
Should you call getFiles() content will still be empty in the end because you use fs.readdir(). This is an async function and will not be able to fill content before it used to build your html page.
You handle every request to your server the same, so you will not be able download any files because you will always just display your page.
You can fix the first 2 easy by using the gist AlexS posted as a comment on your question. The third one will require a bit more setup, but can be made easy if you use Express.
You could add the download tag to your HTML link :
items.forEach(function getItems(item)
{
content+= "<br><a href= " + "\" " + item + "\" " + " download>" +item + "</a><br>";
});
MDN reference
If you dont care about browser support you can use the download attribute in the a tag.
It looks like this :
DL me!
Supported by Firefox 14+ and Chrome 20+
But if you want a general solution then, you can zip your files before sending them, as far as I know .zip are always downloaded when "clicking on them".
You can use 'node-zip or archiver to zip your files.

How to add an variable value in between the hyperlink in javascript

I am sending an email via javascript. In mail.parameter.text property I need to send the hyperlink. In the code below I am hardcoding the url whick looks very lengthy and also I need to add the /dashboard at the end of the url. Any idea on how to shorten the url?
var parent = space.getParent();
var siteGroup = "GROUP_EMAIL_CONTRIBUTORS";
var mail = actions.create("mail");
mail.parameters.to_many = siteGroup;
mail.parameters.subject="New Site Created in Community"
mail.parameters.text=" A new site called " + document.properties.name +"is created.Click http://pc1:8080/share/page/site/"+document.properties.name+"/dashboard"+" to join the site";
//execute action against a document
mail.execute(document);
Sorry if this is a basic question I dont know javascript. This is my entire script and I am not using any html tags in between.
You can change it to something like this, for example:
mail.parameters.text="Click <a href='http://pc1:8080/share/page/site/" + document.properties.name + "/dashboard'>here</a>";
Your link will appear as "here" and nevertheless lead to the URL you specified. Is that what you mean by "shorten the url"?
Please try now.
var url = "Click http://pc1:8080/share/page/site/" + document.properties.name + "/dashboard" + " to join the site";
ail.parameters.text = url;

Categories

Resources