I have a jQuery script in which I want to download a file which is located on some server after the user clicks on the link. On IE11 this works alright, as I simply make a call to:
window.open('file:///MyServer0001/SomeFolder/Tmp/ToiletFlush.log'
I know that for security reasons, Chrome doesn't allow to use the protocol file:/// . It is interesting because if I write myself this URL on the address bar he will show the file, however if I make a call for window.open() he just opens me a new empty window. Also, removing the file:/// , doesn't do anything on Chrome, it says that the page is not available
So right now I don't know how to make this work on chromium. I thought I could make a workaround by calling the controller function through a POST/GET , return a FileResult and download the file. This definitely works but I would like to know a simple way to do this directly only using jQuery/JavaScript.
Any Idea?
Try ASP.NET MVC FileResult:
View:
Download File
In Home Controller:
public FileResult GetFile()
{
//Read file content into variable.
string content=File.ReadAllText("filepath");
byte[] byteArray = Encoding.ASCII.GetBytes(content);
return File(byteArray, System.Net.Mime.MediaTypeNames.Text.Plain, "ToiletFlush.log");
}
Try this,
On Windows:
1. Find the local URL of your Chrome Install
2. From the Command Line launch Chrome w/ '–allow-file-access-from-files' prepended to it:
–allow-file-access-from-files C://...
On OSX, just type this in Terminal:
open /Applications/Google\ Chrome.app --args --allow-file-access-from-files
Using Google Chrome Canary is also possible:
open /Applications/Google\ Chrome\ Canary.app --args --allow-file-access-from-files
Or... You'll need to run your application on a local server.
Related
I am trying to upload a file. My application only has a Browse button and no place to send any file path. So I am unable to use SendKeys.SendWait(#"filepath"); I tried using JavaScriptExecutor but getting error while using the below code:
IWebElement upload = _driver.FindElement(By.XPath("//div[starts-with(#class,'addDoc') and contains(text(),'Browse')]"));
string filepath = #"C:/Users/../Amazon vs Walmart.pdf";
String script = "document.upload.value= " + filepath;
((IJavaScriptExecutor)_driver).ExecuteScript(script);
Runtime.evaluate threw exception: SyntaxError: Unexpected token :
Why am I getting the exception?
Interacting with file browsing windows is outside the scope of what the webdriver can do. When you click browse the browser passes you off to the OS to find the file. Javascript cannot act on these windows.
SendWait is used as a trick to pass the uri of the file to the open the window and return. If that is not working you will need to use something to interact with OS windows.
I personally use InputSimulatorCore to handle issues like this when working with IE, which has lots of these type of issues with file downloads.
Edit: If you control the application changing to a forms based upload will allow you to do the testing in Selenium and is the best option. If not you can also directly do a post to the upload endpoint.
There are multiple issues with the code you posted. First, you’re attempting to call ExecuteScript with invalid JavaScript. To wit, once your strings are concatenated, your JavaScript looks like this:
document.upload.value= C:/Users/../Amazon vs Walmart.pdf
Note that there are no quotes around the string you’re actually attempting to set. The syntactically correct JavaScript would be:
// Note carefully the quotes around the string literal.
document.upload.value='C:/Users/../Amazon vs Walmart.pdf'
To yield that, your concatenation code would need to look like this:
string script = "document.upload.value='" + filepath + "'";
The second issue is that you’re attempting to invoke uploading a file. Assuming the upload is accomplished by using standard HTML mechanisms, that means that somewhere on the page, there is am <input type="file"> element. It might be hidden, but it’s there somewhere on the page. To upload a file, you can use the SendKeys method on that element. The file upload case is one of the very few exceptions for SendKeys to the rule that elements to be interacted with must be visible to the user (at least with the most recent versions of browsers and browser drivers. If the application is using some non-standard upload mechanism, like a pure JavaScript implementation, or some sort of Flash component, then you’ll need to use some other method to communicate with that component.
Though I tried using AutoItX to upload file and it worked. Below is the code for the same -
to get AutoItX, I installed Nugget package - AutoItX.DotNet
AutoItX.ControlFocus("Open", "", "Edit1");
AutoItX.ControlSetText("Open", "", "Edit1", filepath);
AutoItX.ControlClick("Open", "", "Button1");
But still, I would like to explore using JS as well.
Is there a way to redirect my application to a webpage after checking the browser version first?
I'm using C# to run my angular app and the index.html is loaded by default, but is there a way to control that ?
E.g : if my browser is IE load wrongBrowser.html otherwise load index.html (the default one)
Note that i dont want to redirect my page because i want to keep the orignal url : ex localhost/api/search=text. If i do a redirect, it will overide my url. So i just want to load the html content
Im using C# with visual Studio for the server side
The first page of your app will have to load as it needs to be able to determine the browser specs. Only then can you then redirect to another page based on that knowledge.
I have never used Angular JS neither Angular with C#, but from personal knowledge I know you can "redirect" without changing the url, using a XML request in vanilla javascript (maybe you can place this somewhere):
var request = new XMLHttpRequest();
request.addEventListener("load", function(evt){
document.write(evt.target.response);
}, false);
request.open('GET', 'a.html', true),
request.send();
Now what this does is simple, we set variable request which is a XMLHttpRequest object, then we set an event listener for when it loads, after it loads we replace the code in the page with the targets code, we then set the url and send the request.
I have only used this for testing, so there might be issues that I don't know of, but it does import the html code in.
In C# you can do the following using Request.Browser:
if(Request.Browser.Browser.IndexOf("InternetExplorer")){
return View("wrongBrowser");
}
You have to use IndexOf due to the fact that most, if not all browsers return their version in their name too.
Here's a list of possible browser strings:
IE <= 11: InternetExplorer <numeric version>
Edge: Edge <version>
Safari: Safari <version>
Chrome: Chrome <version>
Opera: Chrome <version>
There are more, most of them will come under Chrome though. I will not go into much detail as you specified IE11 which is listed above. The above method is not really reliable for other, less popular browsers so keep that in mind.
this works on dev as usual when I put it on UAT the code does something different. A PDF is saved on the server, the class then opens it up using the url of that file.
The URL works fine if I paste it into a browser, but doesn't work from the code.
Here's my code:
Process.Start(openPath);
openPath will look like: "http://www.cbm360.net/test/temp/CBM360Report_1093750.pdf"
The file is there on the server, but it just won't open in the code.
The code is inside a web method called using AJAX, if that makes any difference, I'm not sure.
Does anyone have any suggestions as to why this isn't working?
The Exception is:
System.ComponentModel.Win32Exception: The system cannot find the file specified
Thanks!
Instead of pasting the URL into a browser (I assume this is on the server), try it directly from the command line on the server. Does it work now? Process.Start is not the same as navigating to a URL in the browser, it is more akin to running the URL from the command line. I am not sure what you are trying to achieve. Normally we would use a web request to get a pdf.
I'm trying to download file from FTP using javascript, for which I created the following topic:
Is it possible to download file from FTP using Javascript?
From there I learned that I can use window.open('ftp://xyz.org/file.zip'); to download the file. It opens a browser new window, but the window closes immediately.
How I can I force it to stay open?
Actually I do all these in Silverlight application:
Here is the code:
HtmlPage.Window.Eval("window.open('" + url+ "', 'Download', 'height=500,width=800,top=10,left=10');");
I also tried this,
string targetFeatures = "height=500,width=800,top=10,left=10";
HtmlPage.Window.Navigate(new Uri(url), "_blank", targetFeatures);
But both results in same : it opens a window, and closes it immediately. I see it just for fraction of second!
I know this doesn't answer your question, and I'm sure you know all of this. I'm answering more because I don't see this point brought up often. :)
Silverlight has very limited support for client interactions. Javascript is a shim that in my opinion gets overused to try and bypass things that Silverlight was architectured against. It would have been very easy for Microsoft to include FTP support in Silverlight but it was excluded for a reason.
However, Silverlight has great support for webservice interactions. So the recommended way of getting a file would be to call a webservice that would do the FTP transfer for you and then send the contents down to the Silverlight application via the webservice. Possibly even processing it on the webservice side for any business logic etc.
Like I said, I suspect your requirement is to not use a webservice (to pass the bandwith cost onto the user most likely). But it'd be interesting to know more about your business problem instead of your technical problem for the solution you've chosen.
It closes because it triggers file download. You can open two windows - one for message and one to download file, but I thiunk user will know it is downloading...
If I were you, I'd open up a page that has whatever visual/UI stuff you'd want to show the user, and either have a META tag that redirects to the download URL, or has a javascript blurb to fire off said download. That way, your window will stay open, but the download will still start automatically.
to keep it open use
var test = window.open();
test.location = 'ftp://openbsd.org.ar/pub/OpenBSD/2.0/arc/kernels/bsd.ecoff';
and to not open any window use
window.location = 'ftp://openbsd.org.ar/pub/OpenBSD/2.0/arc/kernels/bsd.ecoff';
or make a normal link
Remember that a browser is not meant to "display" (visually anyway) the FTP protocol, and not all browsers will suport it. If you want to allow the user to download something, consider using a normal http:// protocol, and opening a window normally as others have suggested.
If you really need the download to be hosted via FTP, consider your backend ingesting (and caching) the file and return it to the user via http
There is nothing to be parsed on the browser's side, hence it closes. If you want to have the page open, you'll have todo something dirty. Like creating a html (or php) page and serve the content you want the user to see, then with a hidden i-frame which will call the FTP contents.
This way your user will see the content you want them to see, and the file is being downloaded.
I had the exact same problem, Silverlight opening a new window for downloading a file would flash a blank window up briefly and it would disappear again without the file download occurring.
This seemed to happen in IE 8 (not 9 and up) and could be fixed by going into Tools->Internet Options->Security then click Custom level... (for whatever zone your site would be in) and go to Downloads->Automatic prompting for file downloads and make sure this is Enabled (I also have File download enabled below that). This Automatic prompting for file downloads setting seems to be absent from IE 9+.
Another workaround is to not open in a new window, if the target url immediately downloads a file it won't change the current window so there's no difference in UX:
HtmlPage.Window.Navigate(new Uri("\download.ashx?fileid=12345"));
I know there is built-in Internet explorer, but what I'm looking for is to open Firefox/Mozilla window (run the application) with specified URL. Anyone can tell me how to do that in C# (.nET) ?
You can do this:
System.Diagnostics.Process.Start("firefox.exe", "http://www.google.com");
This will launch the system defined default browser:
string url = "http://stackoverflow.com/";
System.Diagnostics.Process.Start(url);
Remember that Process.Start(url) might throw exceptions if the browser is not configured correctly.
See ProcessInfo.UseShellExecute
Use the Process class (System.Diagnostics) using the URL as the process name. This will use the system default browser to open the URL. If you specify a browser, you run the risk that the browser doesn't exist.
In Visual Studio click the File -> Browse With... on the menus and then select the browser that you want to use. You can also change the browser there. If the Browse With... menu option doesn't appear then you need to select a project from your solution that that can be launched in a browser.
If you explicitly do not want to use the User's default browser, you can run the browser with the URL as the first argument.
C:\Program Files\Mozilla Firefox>firefox.exe http://google.com
launches Firefox with google for me. But as people have said, you run the risk of it not being installed, or being installed to a different place, etc.