C# download special character file name files from server - c#

Hi I have my files in server and file names on DB ....files without special characters I am able to download from server using html download by binding href ....but for special characters file name its not possible to download..please give me suggestion to do...
I have tried using linkbutton ...which I already created controls in aspx page and binding file name to it...but for multiple files its not working ....all files getting single click ...which file is clicked I am not able to get it...please help me in this

You need to use Url.Encode to create URL
Click Here to download the file

Related

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.

Upload files in RadAsyncUpload control from CodeBehind

I would like to know if its possible to upload files in RadAsyncUpload control from codebehind.
Instead of pressing "Select" button of RadAsyncUpload control and selecting files and uploading it, I would like to upload a list of files present in a directory into RadAsyncUpload control from codebehind.
If we can upload files from code behind, is it possible for you to show me a demo.
Please note, i have RadAsyncUpload control as a usercontrol registered in a page.
Thanks.
The upload process through the codebehind is not allowed.
RadAsyncUpload is based on the HTML input type=file element, which allows to upload files only from the client's machine. The user is allowed to select a file from his/her machine and upload it through the select file dialog of the input element.

How to add an download link to users in my project

In my project i will be having an link like
Download
I want the users to download files of different types. The file will be in the root folder. When i am clicking on the link it is displaying an error. This is the plugin to install in the chrome. If the user download this link and open then it will automatically add to the chrome.
How can i do this.
The file is not even downloading.
This isn't a valid path:
~/hello world.crx
The ~ character is for use server-side to denote the root of the application. Client-side it has no meaning. The browser doesn't know what the root of the application is (or what the application is at all), it's just sending requests to resources at addresses. And it doesn't know what to do with that address.
You'll need to either use some server-side logic to translate that path into a browser-useable path, or manually make it a relative or absolute path.
If the ASP.NET MVC Framework isn't translating this for you then you're probably using a version that requires a little more manual work for it. Try something like:
Download
(Note: This assumes the use of the Razor view engine. If you're not using that then you'll want to use whatever your view engine equivalent is.)
What you need to do is set up a directory online, where you can host the file.
I also see that in your aref you don't want to type the full path so denote it with a /hello_world.crx, but make sure that you've set up a base href:
<base href="http://yourdomain.com/something/">
Try renaming the file to remove any spaces e.g. "hello_world.crx" and then change the name in the link code to match.
if a webpage and the downloadable file is in the same location
(i.e)
SampleFolder->Download.html
SampleFolder->hello world.crx
then try the below
download
If the webpage and the downloadable file in different location
(i.e)
SampleFolder->Download.html
SampleFolder->Downloads->hello world.crx
then try the below
download

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.

How do I read a file located in a same folder where my page resides in in ASP.NET?

How do I read a file located in a same folder where my page resides in in ASP.NET (C#)?
I have a page called mypage.aspx and I'm trying to read in a file called foo.txt residing in a same directory as this page.
Is there a way to open that file for reading with File.OpenRead()?
Providing a relative path like File.OpenRead("foo.txt") fails b/c of the location of the file.
It should be something like
File.OpenRead(Server.MapPath("foo.txt"));
You should try File.OpenRead(Server.MapPath("foo.txt")).
If MapPath doesn't expand/can't find the proper path at this point then try it while specifying the relative path to the page in question starting from the sites virtual root (using the tilde (~) at the beginning of the string to indicate this), i.e. File.OpenRead(Server.MapPath("~/path/foo.txt"))
In ASP.NET the folder is really IIS's folder which is typically in C:\Windows\System32\Inetsrv\ etc.
What you will need to do is use either
Server.MapPath("TheFileName").
Or get the PhysicalApplicationPath from the Request using
Request.PhysicalApplicationPath
or
HttpRuntime.AppDomainAppPath
and go from the Request and then go from there
You can use a label message or textbox in the aspx page and you can display the file in that by using the below code, I had used a label message wit lblDisplay ID.
lblDisplay.Text = File.ReadAllText(Server.MapPath("Give the path here"));

Categories

Resources