I have some problem with MS Edge 20.10240.16384.0 (the newest versions has the same error).
I'm trying to download file with JS (clear JS, I have no opportunity to use any libraries). Here is the way I'm doing this:
window.open(url);
Where url variable is an URL to file which I want to download. Opening that URL instantly causes file download. The problem is that the downloaded file has no extension and it's name looks like
'=_UTF-8_B_dGVzdEZpbGVOYW1lLV9kc2FkLnBkZg==_='
The original file name is 'test.pdf' and my method works perfect with Chrome, Firefox and IE11.
Are there any solutions?
Thanks.
Took me a good few hours and found out that if file name has '£' character, the file gets download as txt with random characters as the file name.
Added code in the API to replace '£' with 'GBP' for the file name.
File download works perfectly fine now.
Weird thing is that I check all characters against Path.InvalidPathChar() list provided with System.IO.File class. Yet '£' throws an error. And that's also why I couldn't get the issue right away as I know '£' is an allowed character.
File download tested with ASP.Net MVC WebApi.
Both IE and Edge keep failing but Chrome and Mozilla works without an issue.
Somehow to fix that error in MS Edge. We can do something stupid like this.
I don't know about the your server's programming language. So I simply post you a flow-chart about it for you to implement it.
Here's a client code:
(I am sorry that I used a library (jQuery))
var uid = "12093ujdskf3";
var url;
$.ajax({
dataType: "json",
url: "http://www.example.com/download.jsp",
data: {
uid: uid
},
error: function(){
url = null;
},
success: function(data){
url = data.url;
}
});
if (url == null){
alert("Error occurred!");
return;
}
window.open(url);
Related
I have created html5 based app in Titanium for ipad. Using Titanium I have stored a file named demo.txt in
/Users/anonymous/Library/Developer/CoreSimulator/Devices/FE1CFXXX0AC-D5BD-9615-C58D80B5A9C6/data/Containers/Data/Application/2D25XXXX-4687-B28A-1EA7B7EA3013/Documents/
in same application itself. Now I need to want to access content of demo.txt in my index.html. Anyone having idea regarding this please help me out.
You can use similar technique to read a file:
var pagesDetailFile = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "demo.txt");
alert(pagesDetailFile.read()); // Read the file
pagesDetailFile = null; // remember to release the file
You should read the documentation carefully for available functions.
You will have to use the techniques described at https://appcelerator.github.io/appc-docs/platform/latest/#!/guide/Communication_Between_WebViews_and_Titanium to fire an event in the WebView HTML which you'd pick up in Titanium JS, get the file and then use evalJS or fireEvent to send it back to the WebView HTML.
I used jquery in order to read data from the simulator and it works for devices too.
var filePath = "/Users/anonymous/Library/Developer/CoreSimulator/Devices/FE1CFXXX0AC-D5BD-9615-C58D80B5A9C6/data/Containers/Data/Application/2D25XXXX-4687-B28A-1EA7B7EA3013/Documents/admin/content.txt";
$.ajax({
url: filePath,
context: document.body,
async: false,
success: function(response){
Ti.API.info("FILE CONTENT " + response);
},
error: function(data){
alert('does not exist');
}
});
filePath is actual path in which we are storing data.
I have created an extension for mozilla firefox. Now, I'm trying to distribute the extension on an simple web site. I generate a sha1 hash code from an online generator. This is the code I have in my web site:
<script type="application/javascript">
function install (aEvent)
{
for (var a = aEvent.target; a.href === undefined;) a = a.parentNode;
var params = {
"Foo": { URL: aEvent.target.href,
Hash: aEvent.target.getAttribute("hash"),
toString: function () { return this.URL; }
}
};
InstallTrigger.install(params);
return false;
}
</script>
<a href="c:/grouAndUsersWorkSpace/MozillaAddon/createtab.xpi"
hash="sha1:a7093a2afe1a53fde114a4a7dcb3e15e57862642"
onclick="return install(event);">Install Extension!</a>
the path of the url is local. And as a result when I start the application I got "The add-on could not be downloaded because of a connection failure on localhost".
I changed the path of the url to be: file://c:/grouAndUsersWorkSpace/MozillaAddon/createtab.xpi and with this nothing happens.
I have two questions:
1. Is that a good way to generate a hash code?
2. What should cause that connection failure?
1) I prefer to use the CHK Checksum Utility to generate checksums.
2) I don't have access at the moment to verify it, but have you tried serving the extension with Apache or similar?
Edit
Since you used a local file, you'll need a 3 slashes instead of 2 : file URI scheme .
Tested it both ways, they both work.
Summary
I am attempting to find out why the wl.download function will not download more than one file even though the Microsoft examples seem to indicate that they can.
And, the code seems to be called for each file you attempt to download, but only the one file is actually downloaded.
Details
Here are the details of how you can see this problem which I've tried in IE 11.x and Chrome 30.x
If you will kindly go to :
http://isdk.dev.live.com/dev/isdk/ISDK.aspx?category=scenarioGroup_skyDrive&index=0
You will be able to run an example app which allows you to download files from your skydrive.
Note: the app does require you to allow the app to access your skydrive.
Once you get there you'll see code that looks like this on the right side of the page:
Alter One Value: select:
You need to alter one value: Change the
select: 'single'
to
select: 'multi'
which will allow you to select numerous files to download to your computer. If you do not make that one change then you won't be able to choose more than one file in the File dialog.
Click the Run Button to Start
Next, you'll see a [Run] button to start the app (above the code sample).
Go ahead and click that button.
Pick Files For Download
After that just traverse through your skydrive files and choose more than one in a folder and click the [Open] button. At that point, you will see one of the files actually downloads, and a number of file names are displayed in the bottom (output) section of the example web page.
My Questions
Why is it that the others do not download, even though wl.download is called in the loop, just as the console.log is called in the loop?
Is this a known limitation of the browser?
Is this a known bug in skydrive API?
Is this just a bug in the example code?
The problem here is that the call to wl.download({ "path": file.id + "/content" }) stores some internal state (among other things, the file being downloaded and the current status thereof). By looping over the list of files, that state is in fact overwritten with each call. When I tried downloading three text files at once, it was always the last one that was actually downloaded and never the first two.
The difficulty here is that the downloads are executed in the traditional fashion, whereby the server adds Content-Disposition: attachment to the response headers to force the browser to download the file. Because of this, it is not possible to receive notification of any kind when the download has actually completed, meaning that you can't perform the downloads serially to get around the state problem.
One approach that I thought might work is inspired by this question. According to the documentation, we can get a download link to a file if we append /content?suppress_redirects=true to its id. Using this approach, we can set the src property of an IFrame and download the file that way. This works OK, but it will only force a download for file types that the browser can't natively display (zip files, Exe files, etc.) due to the lack of the Content-Disposition: attachment response header.
The following is what I used in the Interactive Live SDK.
WL.init({ client_id: clientId, redirect_uri: redirectUri });
WL.login({ "scope": "wl.skydrive wl.signin" }).then(
function(response) {
openFromSkyDrive();
},
function(response) {
log("Failed to authenticate.");
}
);
function openFromSkyDrive() {
WL.fileDialog({
mode: 'open',
select: 'multi'
}).then(
function(response) {
log("The following file is being downloaded:");
log("");
var files = response.data.files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
log(file.name);
WL.api({
path: file.id + "/content?suppress_redirects=true",
method: "GET"
}).then(
function (response) {
var iframe = document.createElement("iframe");
iframe.src = response.location;
iframe.style.display = "none";
document.body.appendChild(iframe);
},
function (responseFailed) {
log("Error calling API: " + responseFailed.error.message);
}
);
}
},
function(errorResponse) {
log("WL.fileDialog errorResponse = " + JSON.stringify(errorResponse));
}
);
}
function log(message) {
var child = document.createTextNode(message);
var parent = document.getElementById('JsOutputDiv') || document.body;
parent.appendChild(child);
parent.appendChild(document.createElement("br"));
}
Did you try to bind some events to the WL.download() method? According to the documentation:
The WL.download method accepts only one parameter:
The required path parameter specifies the unique SkyDrive file ID of the file to download.
If the WL.download method call is unsuccessful, you can use its then method's onError parameter to report the error. In this case, the WL.download doesn't support the onSuccess and onProgress parameters. If the WL.download method call is successful, the user experience for actually downloading the files will differ based on the type of web browser in use.
Perhaps you are getting some errors in your log to identify the problem.
For me, one suggestion without having checked the documentation, I can think of the fact that you are not waiting for each download to end. Why not change your loop in such a manner that you call WL.download() only if you know no other download is currently running ( like calling the next WL.download only in the success/complete event ):
WL.download({ "path": file.id + "/content" }).then(
function (response) {
window.console && console.log("File downloaded.");
//call the next WL.download() here <!-----------------
},
function (responseFailed) {
window.console && console.log( "Error downloading file: " + responseFailed.error.message);
}
);
I have some images in a folder with the .JPG extension. I want to change the image extension to .PNG programmatically. My earlier post is here: https://stackoverflow.com/questions/15428521/read-a-file-extension-and-change-that-extension.
$.ajax({
type: "GET",
url: "aa.jpg",
dataType: "snapshot",
success: function (snapshot)
{
try
{
var src = $(this).attr("url");
$(src).attr('src',$(this).attr('url').replace('.jpg','png'));
}
catch(ex)
{
alert(ex);
}
}
});
After reading both your question I realise that you want to rename files on the client.
This cannot be done using jQuery alone. Normal javascript engines do not allow access to filesystems on client side.
You need something a little more powerful - on windows that would be an ActiveX object. If you're on another OS, or a browser which does not support ActiveX, I don't know how to help you.
Here you can find an example.
i have a problem with the the downloaded files in my PhoneGap-App for Android.
The download-Function from PhoneGap actually works quite well i think. It gets the file from the URL and
stores it on the SD-Card. (Code is below)
So where is the Problem? When I download a JPG or PNG to the
Download-folder, i want it be accessible through the native Gallery.
But the picture don´t appear in the gallerie. To see it I have to
restart the phone or I have to use another App like Astro.
Is there a "Refresh_the_native_Gallerie"-Function or something like that?
Thank you very much.
try {
var filePath = 'file:///mnt/sdcard/Download/google.png'; // Correct filePath
var url = "https://www.google.de/images/srpr/logo3w.png"; //Correct URL
var fileTransfer = new FileTransfer();
fileTransfer.download(url, filePath, function(entry) {
console.log("s3_download download complete: " + entry.
// Do i need a "Refresh_all_other_Apps"-function here?
}, function(error) {
// Normally no Error
console.log("s3_download download error source " + error.source);
console.log("s3_download download error target " + error.target);
console.log("s3_download download error code" + error.code);
});
} catch (e) {
console.log("downloadTest ERROR: " + e);
}
Cannot answer my own question so fast only edit my question. So here is the answer:
I wrote a little PhoneGap-Plugin, which does actually nothing more than calling the code from zapl. Thanks again. Hope it will help someone with the same problem.
You can find the code here: https://github.com/philipp-at-greenqloud/pluginRefreshMedia
I don't know if that helps you in PhoneGap / JavaScript context:
Your problem is that the gallery will only display files that are indexed in the device media-database. Just adding a file to the file system will not automatically add it to that database. And pretty much the only time a rescan / update of that database happens is when you reboot the device or remount the sdcard (after it was shared with a PC or in case you ejected it and put it back in).
To have a file added to the database the simplest way to to that is to send Intent#ACTION_MEDIA_SCANNER_SCAN_FILE to the MediaScanner to let it add your file to the database. Once that is done the file will show up in the gallery.
Java code for that would be
File newImage = new File("/mnt/sdcard/Download/google.png");
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.fromFile(newImage));
sendBroadcast(scanIntent);
I wrote a little PhoneGap-Plugin, which does actually nothing more than calling the code from zapl. Thanks again.
Hope it will help someone with the same problem.
You can find the code here: https://github.com/philipp-at-greenqloud/pluginRefreshMedia
This is a well needed plugin. I have been trying to do work arounds for this issue and the plugin works a treat. I was looking at developing one myself, but you have done a great job. My headaches are now subsiding. This works for me.