Providing links to files outside of IIS - c#

I have an upload function in my system that stores the files on the d drive e.g D:\KBFiles. Now i need to offer these files as links through internet explorer. Obviously i cant just offer a path e.g D:\KBFiles\test.pdf. Whats the best way to handle this scenario

Write "proxy" file with such code and call it DownloadFile.aspx:
string fileName = Request.QueryString["file"];
string filePath = Path.Combime("D:\\KBFile", fileName);
Response.WriteFile(filePath);
Then have such link:
test.pdf
This allows you to check the user permissions if you're using Login system and also you can check the requested file against some white list to prevent hacking attempts.

You need to create a Virtual Folder for that windows folder inside your WebApplication. As soon as IIS has mapped the virtual folder, it would be possible to use direct links, which would have your WebApplication as a root.

Related

ASP MVC Rotativa (wkhtmltopdf) to Virtual Directory

I don't want to raise this as an issue / bug on the git page as I imagine I am simply using the package wrong.
I have an ASP MVC site running on IIS10. Actual folder structure is:
x:/
library/ <-- Physical
Web Sites/
Site A/
Files/ <-- Virtual
Site B/
Site C/
And using IIS10, I've made a virtual folder called Files under Site A, of which the physical path is X:/library/ (the reason for this is to share resources between sites and to take the content outside of the web-site path).
Users are able to upload / download files to / from the virtual directory fine. I can load images from there without issue also.
However, I cannot seem to get Rotativa to write to the virtual directory from within my application. Obviously command line does not see the virtual directory anyway, but creating a file from cmd to the physical directory works fine.
From within the app though:
X:/Web Sites/Files/Test.pdf results in Unable to write to destination
X:/library/Test.pdf results in Access to the path is denied (despite already giving NETWORK SERVICE full control of the library folder and sub-folders)
The code I am using is:
var pdf_output= new ActionAsPdf("Summary", new { id = id }) {
FileName = String.Format("Summary_{0}.pdf",id.ToString()),
CustomSwitches = "--print-media-type"
};
byte[] pdf_data= pdf_output.BuildPdf(ControllerContext);
System.IO.File.WriteAllBytes(String.Format("xxx",id.ToString()), pdf_data);
with a variety of directory path combinations in place of "xxx".
Has anyone had any experience with this before that might show me where I am going wrong? Any help would be greatly appreciated.
Never mind, solved it. I needed to grant the respective Application Pool permission to the physical library directory by adding the user IIS AppPool\Site A.

To browse folder and Create folder in the server and upload files using asp.net

I am Currently working on asp.net i have worked on file upload by uploading files but i want browse a folder not a file and get all files in it and get foldername and create foldername in server and upload files it to server.Pls give me some refernce and help me to do this.
My aim is when i browse that folder all it files present in it should upload .Only files should upload not folder
Note:To browse folder not file.
Actually you are supposed to include a question if you want an answer, but it seems you are completely new to this so here are a few things you should know when it comes to asp.net and folder/file handling:
Your virtual path always corresponds to a local path on your webserver. Since it would be bad to hardcode that you might want to start off by mapping it. (e.g. /uploads/ to _C:\intepub\application\uploads)
"and get foldername"
string localpath = Server.MapPath("/uploads");
"and create foldername in server"
next you can check if that folder already exists, or if not - create it.
if(!System.IO.Directory.Exists(localpath))
System.IO.Directory.Create(localpath))
Keep in Mind though that your IIS-User needs the rights to create new directories
Now that you have made sure that your upload directory exists, you can upload/download files. Here is a good tutorial to get started:
"and upload files it to server"
http://msdn.microsoft.com/en-us/library/aa479405.aspx
"but i want browse a folder not a file and get all files in"
You can do that on the server, not the client. Your asp.net code never executes on the client.
string[] filenames = System.IO.Directory.GetFiles(localpath);
"My aim is when i browse that folder all it files present in it should upload"
That one isn't so easy, sorry. It would pose a serious security risk to users.
Maybe this will point you in the right direction (apparently it works with Chrome)
How do I use Google Chrome 11's Upload Folder feature in my own code?
When it comes to files, directories, filepaths, etc. in general, I would recommend taking a closer look at these classes:
System.IO.File
System.IO.Directory
System.IO.Path

C# Upload Files on another partition of the server

I'm using FileUpload.SaveAs() function of C# to upload files to the server but I want to save the files on another partition. Let us say, save the files on Drive D of the server instead on the current drive which is Drive C. Please share your thoughts. Thanks is advance.
I have learned that using full path such as
FileUpload.SaveAs("D:\FileUpload");
will save the file outside the web server.
Check this out.
To simplify the question, how can I upload files on the other partition of the server that hosts my web app?
Based on the documentation from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas.aspx, the String filename is the full path name of the location to save. Meaning you should be able to do so e.g:
FileUpload.SaveAs("D:\where_you_want_to_save")
By the way what have you tried and what error did you get?
Looking at the example on MSDN, it would appear that .SaveAs() accepts a fully qualified file name as a parameter. You could potentially use a Path object to cleanly build a path for the file, or just specify one directly as a string:
uploader.SaveAs("d:\\someFolder\\someFile.ext");
Resolved this by using Virtual Directory of IIS and providing admin credentials for authentication

Upload images acces to path denied

Hi I seem to be having a problem when uploading images in asp.net.When I tryed to upload an Image I get this error:
Access to the path 'D:\Projects IDE\Visual Studio\MyWork\Websites\Forum\Images\avatar\userAvatars\aleczandru' is denied.
I have set application pools Identoty to NETWORKSERVICE ando also added the NETWORK SERVICE account to the Images folder with full permision but I still get the same error.
This is my code:
private void addImageToApp()
{
string path = "~/Images/avatar/userAvatars/" + User.Identity.Name;
createPath(path);
if( Directory.Exists(HostingEnvironment.MapPath(path)))
{
//try {
UploadImage.SaveAs(HostingEnvironment.MapPath(path));
// MultiViewIndex.ActiveViewIndex = 0;
//}catch(Exception ex)
//{
// AvatarDetails.Text = ex.Message;
//}
}
}
private void createPath(string path)
{
string activeDir = HostingEnvironment.MapPath("~/Images/avatar/userAvatars");
if( !Directory.Exists(Server.MapPath(path)) )
{
string newPath = Path.Combine(activeDir, User.Identity.Name);
Directory.CreateDirectory(newPath);
}
}
What else can I do to solve this problem?
EDIT
Hi at this point I have full permision control to the following USERS:
Authetificated Users
IUSR
SYSTEM
NETWORK SERVICE
IIS_WPG
Administrator
USers
Is it posible that I need to set any configuration to IIS in order for this to work?
EDIT
I have messed around with SQL-SERVER for the last couple of days in order to make this work so I might have missconfigured something form what I understand NETWORK SERVICE is stored in SQL-SERVER master.db database.I seem to be having two network service logins may this be the problem?I remember when I first checked it I had none now I have two:
EDIT
This is the print with the permisions I added to the folder:
EDIT : Complete error
StackTrace:
In method CreatePath you are creating folder 'D:\Projects IDE\Visual Studio\MyWork\Websites\Forum\Images\avatar\userAvatars\aleczandru'.
Then, you try to save the uploaded image with the filename 'D:\Projects IDE\Visual Studio\MyWork\Websites\Forum\Images\avatar\userAvatars\aleczandru'.
You can't have a folder and a file with the same name. If you try to do this, the OS will tell you access is denied.
I suppose you want to either create a filename inside folder aleczandru, or you meant to save the file as aleczandru.png or something in folder userAvatars.
Assuming your UploadImage is a FileUpload control, you can save the file to the user's folder using the original file name of the uploaded file.
UploadImage.SaveAs(HostingEnvironment.MapPath(
Path.Combine(path, UploadImage.FileName)));
Pls make sure you have full filename with file extention in you path.
Ok... I have done this before for a project to implement a PUT method for http. I dont clearly remember it.. but some hints... if I were in my office I could tell you correctly. here are the hints
You need to add IIS_IUSRS to have access to the folder in windows.
Go to IIS admin console click the deployed site node, and set the permission for the same folder/website requests coming in... I dont remember the which category was it.. that settings pane will allow you to add/modify permissions for POST, GET and other verbs for that matter... when you edit that, you should see options for Administrator, a particular user account, anonymous etc.
may be I will write back tomorrow... exactly how to do it :-)
Try to give the group called users the permission to modify this directory (under security)
You need to find out what user the asp.net upload page is running under. If you haven't changed it, and are not running under impersonation, it should default to the ASPNET user on the local machine. Whatever it turns out to be, give that user read/write permissions on the folder.

Download and save aspx page

I saved aspx page as html it worked in my local machine but after published on the server its showing an error that "the access to the path is denied"... I tried giving access permission then also it doesn't work.. can anyone help with it? or else is there any other way to save the page in C# asp.net?...
string url=HttpContext.Current.Request.Url.AbsoluteUri;
string sHtml="";
HttpWebRequest request;
HttpWebResponse response=null;
Stream stream=null;
request=HttpWebRequest)WebRequest.Create(url);
response=(HttpWebResponse)request.GetResponse();
stream=response.GetResponseStream();
StreamReader sr=new StreamReader(stream,System.Text.Encoding.Default);
sHtml=sr.ReadToEnd();
string path=Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string textfilename=TextBox1.Text;
string getpath=path+"\\"+textfilename+".html";
File.WriteAllText(getpath,sHtml);
if(stream!=null)stream.Close();
if(response!=null)response.Close();
Thanks..
If you need to save a file use this to get the application path
Server.MapPath(#"filename.txt");
Try this because when you only write the filename at an web server you are trying to access to a Microsoft.Net folder.
yes make sure the folder you write to has the appropriate privileges. IIS 5 uses ASPNET account, IIS6 uses network service. Just give read/write to the proper account. Is this your dev environment, or a prod environment?
Also, make sure it's pointing to a folder; desktop folder for ASPNET account probably won't exist since ASPNET account isn't a user with a users folder.
I had a similar problem when trying to deploy CSS from a ZIP downloaded from the web. Try right-clicking on the aspx file from windows explorer and checking the file properties. One of the tabs may indicate that the file has been "blocked". If so, there will be a button there to unblock it.

Categories