Showing file on browser - c#

I want have two links that to download and view the files
I am able to download the file directly. If user selects to view option I want to open that file in browser new tab.
How I will do it in asp.net mvc?
For download I have below action
public FileResult Document()
{
return File(filepath, contenttype,filename);
}

You can't control whether or not the file opens in a new tab or an existing tab from the server, this is controlled by setting the target attribute on the link definition.
You can control whether the browser downloads or attempts to display the file by setting the content type.

Related

How do I redirect to a static HTML page from disk when clicking a link on an ASP.net website?

I was asked to make a downloads page for our company intranet. They want a folder on the server in which they can drop files for others to download. The way I did this was to create an ASP.net website which looks at that folder and creates list of hyperlinks for every file in that folder. Every one of this hyperlinks triggers the function to present that file for download to the user.
Here's the problem. Some of those files are static HTML pages and the users want to navigate to those pages instead of downloading them. So I used the following code
FileInfo file = new FileInfo(pathToFile);
if (file.Extension.Equals(".htm") || file.Extension.Equals(".htmls"))
Response.Redirect("file:///" + file.FullName);
else { /* run the download code */}
The download part works fine. Clicking on an html link does nothing and I can't figure out why.

Download file from protected website programmatically with Save As popup

I use WebBrower of microsoft to log into a website. I have no problem with logging in. I don't have the downloadable file url because the file is generated automatically.
How should I download file with out Save As popup to show ?
Work with the DOM (document object model) of the website find your download link via traversing the DOM exposed by the browser control via it's property
Document
it is a tree of elements that your after-login page consists of. There should be some element that you should invoke click on it and then handle OnNavigating (https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.onnavigating(v=vs.110).aspx) event of the control to get the generated download URL.

Get the UNC of a file that the end user selects

I am trying to create a Sharepoint webpart for a user to browse to, and select, a file from a share on our file server. Then I need to create a link to this file to display in a list of links that will go onto our Sharepoint intranet page. I have created a custom web part using asp.net/c# in order to do this but I am stuck on how to get the UNC path to the document. From my understanding it wont work with an asp.net fileupload control or a html input element. What other options would there be? I really don't want the user to have to type in the whole path to the document. This needs to be a re-usable solution so that my users can create new lists of links to documents when they so desire. Thanks for any advice.
I don't think the full file path is supported or allowed via modern web browsers via the file upload control. What you would end up having to do is create something server-side that use a service account to access the file share, and then the client (web page) could call the server-side code as it traverses the file share until the user selects a file.
Example:
server: on load, here are the contents of "\server\home"
client: show the contents of subfolder "\server\home\pictures"
server: connects to "\server\home\pictures" and returns contents
client: selects "\server\home\pictures\foo.jpg"
Check out System.IO.Directory http://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.100).aspx for ways to get listing of directory contents server-side (GetFiles, GetDirectories, GetFileSystemEntries, etc) and then you can return those results to the client.

Downloading a file from url in c# windows forms

I have url like this
http://domainname.com/view/downloadfile?uname='ddd'&id=4
if we type the above url (not exactly the same ) in browser address bar it prompts open/save file dialog
my requirement is in button click without open/save dialog i need to download the file to my local disk location
Actually am working with webbrowser control .button is outside the webbrowser control
You could use the WebClient.DownloadFile method. Or if you don't want to save the file on the disk but manipulate it in memory you could use the WebClient.DownloadData method.

how to create folder on server from code in c#?

Is there any way to show user popup to give the name to create folder
And with that if he does not want to create folder then he should be shown the list of folders to which he want to save the file
Is it proper way that I create a popup with javascript with the list of folders on my server and put a textbox and button to save the new and in code we will create directory in code passed with textbox?
string pathToCreate = "~/UserFolders/" + TextBox1.Text;
if(Directory.Exists(Server.MapPath(pathToCreate))
{
//In here, start looping and modify the path to create to add a number
//until you get the value needed
}
//Now you know it is ok, create it
Directory.CreateDirectory(Server.MapPath(pathToCreate));
string targetPath = Server.MapPath("FolderName"); //with complete path
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
fileToUpload.PostedFile.SaveAs(targetPath + ImageFileName);
}
else
{
fileToUpload.PostedFile.SaveAs(targetPath + ImageFileName);
}
As I mentioned by commenting that Functionality like SaveFileDialog is only possible in Windows Forms application, you cannot do this in asp.net
The alternatives in asp.net are difficult however as an alternate there is a third party editor available on the web called CKEditor 3.x Developer's Guide.
You can use File Browser (Uploader) for the purpose you want to achieve.
to create folder :
System.IO.Directory.CreateDirectory(newPath);//newpath is your folder creating path
If you are developing web applications (I can see you have tagged your question as asp.net), you do not need to worry about the pop up window. You just send the file through the Response and the download window will be shown by the browser.
Using the Example given in How to Create a Folder or File You can create a folder, but in asp.net you can not Use open file dialog or save file dialog functionalities, this will work only in C# Windows forms application.
Web applications and the HTTP protocol do not support direct manipulation of the server-side file system. In fact, that would be a dangerous idea.
It sounds like you have a base folder somewhere on the web server where you want users to be able to upload files, with some user control over subfolders and location of the uploaded file. I would suggest dong this by presenting the file system in a tree view or a list of links (where the links let you navigate up/down the folder tree). Combine this with a file input and you've got yourself a solution. HOWEVER, be very careful to control and cleanse the specified file name for the uploaded file. There are many combinations (such as utilizing "..") that can allow the user to hack into unwanted file locations on your server.

Categories

Resources