create file on localhost (Web server physical path) - c#

Currently I am working on asp.net website and I am not very much familiar with asp.net. I have a WebForm and when user clicks on Submit button a .txt file should be get created on server(Text File name is ID entered by user which is unique.)
I used following to create the file.
File.Create( Server.MapPath("~") + id + #".txt");
But this gives me error when the site is running on the localhost of my pc.
Access to the path 'C:\inetpub\wwwroot\XXX\YYY.txt' is denied.
This is something related with access permission. What should I do to fix this. Please advice me.

Add write permission for NETWORK SERVICE to the folder in question.

File.Create(#"\\127.0.0.1\www\test\test.txt");

Related

ASP.Net "Access to the path '' is denied."File Upload Control not Working in Web Server (IIS7)

I'm currently developing a website application and one of its features is to be able to upload and download the files. All the uploaded files will be stored in the external network location. First is on page load, it will create a temporary folder and all of the uploaded files will be stored there. Then when they click the save button, it will create a folder where it will be permanently stored and put all the files in the folder and remove automatically remove the temporary folder. It is working fine in my local host server, but when I upload it and put to web server, it throws an error, Access to the path '(network path)' is denied.
ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
I've researched for a solution for almost a week now and it says it's something about permission, but all of the solutions on the net are not clear and doesn't work for me. I hope someone could help me with the step by step instruction on how to configure permissions to be able to allow network access o any possible working solutions.
UPDATE:
Here is the current screenshot:
Permission for the web site in IIS7
I hae downloaded the ASP.NET components but I can't find the ASP.NET account. I have .net 4.0 application pool. Should I give permission on my uploading folder? Please help me.
Thank you everyone.
This is a permission issue and you already have a hint :
To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
Explore to IIS manager.
Expand Sites node and find your site then right click.
Click Explore and you will be redirected to the physical file path.
Right click the folder and go to security tab
Click Edit button (another window will pop up)
In this window add the iis user normally it is prefix with IIS_MACHINENAME
Set full permission to this user and you are good to go.
I struggled with this same issue and while we did all the permission changes shown I was still getting the error. In the end I wrote a small test page using the file upload control and when I tried it with various files it worked fine.
What I discovered was that there was a slight difference between these two file names and that was causing the error
this_file1.ppf
thisfile1.pdf
Yes, the underscore was causing the access violation error and for the life of me I do not know why? This also applies to files with a space in the file name. We are running the webserver (IIS7) on a Windows Server 2012 R2 if that makes a difference.
I did not find any reference to this in the control though it will fail on long file names as well.
Fixed mine by:
File properties
Under the security tab click add
Add the iis user which is in my case IIS_IUSRS and then click Check Names
Click OK and grant full-access from the permissions category
If you cant find IIS_IUSRS you can always go to Security > Add > Advanced > Find Now to find the IIS username.
I've also added the current logged in user by typing in the current logged in username and granted full-access
In my case , i just copied required files to wwwroot folder in c Drive, and changed the physical path , it solved my issue.
Just a hint: in my case, the following code doesn't work and emits the message “Access to the path (...) is denied.”
var Request = HttpContext.Current.Request;
foreach (string file in Request.Files)
{
var postedFile = Request.Files[file];
postedFile.SaveAs(pathToSave);
}
This one works fine:
var Request = HttpContext.Current.Request;
foreach (string file in Request.Files)
{
var postedFile = Request.Files[file];
using (var stream = new FileStream(pathToSave, FileMode.Create))
{
await postedFile.InputStream.CopyToAsync(stream);
}
}

FTP upload in .net — not getting correct file path in some browsers

I'm building an application which involves writing some fields to a database, along with uploading some files from the end user to an FTP site. The file upload works fine... in IE. In Firefox and Chrome, I get an error that it can't find the file (running it in localhost at this point, haven't moved it to a dev or production environment yet).
I have tried getting the file via:
Server.MapPath(FileUpload1.PostedFile.Filename)
... which points to the folder the application is residing in.
And also:
Path.GetFullPath(FileUpload1.PostedFile.Filename)
... which points to c://Programs (x86)/... ...
I can get a file to upload properly if I get it from either folder, but nothing from anywhere else.
Any ideas on how to make this point to the right place? Or, will it actually work properly once it resides in a server environment?
Thanks in advance!
FileUpload.PostedFile.Filename works differently in each browser. in Firefox and Chrome it won't include the full path - just the file name. It depends on your customer's browser.
FileUpload.PostedFile.FileName
This actually gives you path of the uploaded file.
But in all the newer browsers (FF 3.6 series, Chrome, IE7+) this feature has been disabled due to security reasons. Any website should not need path of a file stored in client's systems because that gives the directory structure and may expose other important things to website owner.
So in your case, the above code returned only the file name.
you can check this link, it may help you Fileupload control - fullpath issue

how do I access a folder on the shared hosting space?

I am trying to upload image files to the server and it gives me an error
"System.UnauthorizedAccessException: Access to the path 'D:\Hosting\234344\html\Testingfiles\upload\813.jpg' is denied.at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)"
in the HttpHandler I have :
HttpPostedFile file = context.Request.Files["Filedata"];
string usr_id = context.Request.Form["usr_id"];// gets the JSON data from the request
string strPath = context.Server.MapPath(("/Testingfiles/upload/") + file.FileName);
string ext = Path.GetExtension(strPath);
if (ext.Equals(".jpg") || ext.Equals(".jpeg") || ext.Equals(".png"))
{
file.SaveAs(strPath);
context.Response.Write("Image uploaded successfully");
}
what am i doing wrong here?
The error message says it all. You don't have write access to that folder.
You will need to ask your hosting provider to assign write rights to that folder for the ASP .NET identity.
Also, consider if you can use a folder below ~/App_Data. This is by convention the place to store files that needs write access in ASP .NET, so many hosting providers will allow writes to this folder by default (but you would need to check yourself for your specific host to be sure).
You should try writing to ~/App_Data/ to see if that works. If it does then its just because you haven't given asp.net write permission to the /TestingFiles/Uploads/ folder.
If your control panel has Plesk on it then you can sort this out yourself by going to the FileManager and clicking the permissions button. If you look at your App_Data file permissions for reference, the actual username that you need to add will vary depending on your domain name with plesk.
Other hosting control panels may allow you to do it in different ways.
If you can't find it then you should ask your host how you set up file permissions or look in their knowledge base.
If your control panel has Plesk on it then you can sort this out yourself by going to the FileManager and clicking the permissions button.
I fixed this error by allowing IIS users full access to upload folder. No need to use App_Data folder
For Plesk 12.0 only:
No need to use App Data folder. You just have to give full control to your Application pool group IWPG(username). It will surely work. I searched for many hours and this solution worked for me .
Hope It works for others too.

Folder Link of the uploaded file

Here is my situation
I am developing a project management application in asp.net.
In that when a customer gives the project detail to the employee, he also uploads a file with that (~ 100 mb).
I don't want that to be uploaded by the customer.
We have all the drives connected in a local network.
What I'm thinking is instead of uploading a file he can give the link to the folder location and by clicking on that link in the browser itself the employee would be able to access the file.
How should I implement this OR please suggest some good practice or method to solve this type of a problem.
Since it's all intranet, you could just have the user provide you a UNC path that the ASP.Net Application Pool identity has access to.
In order to provide the file back to the user, you can either provide the UNC path as an href such as:
<a href="file://///server/path/to/file.txt"/>
Or write the file to the response:
Response.Clear();
Response.Buffer= true;
Response.AddHeader("Content-Disposition","inline;filename=file.txt");
Response.Charset = "";

How to open a file stored in a server from aspx page using UNC share path?

I am listing the files stored in a remote server in the aspx page grid view with the file name linked (href) to the UNC path of the file.
when the user clicks the file name link, it should open the file from the server to allow the user read the file.
I am using impersonation (Using mirror logon method) for file upload and delete in the same page which works fine.
When I try to click the link to view the file it throws an error message box saying access denied.
While upoading / deleting file from my application, it uses the impersonated account I specified in web.config.
But it uses default ASP.NET (SERVER\IUSR_SERVER) account when trying to access via the link.
I cant give read access to all in my file server share.
How can I make the application use the impersonated account while trying to access the file for reading through the link I am showing for the files?
Or please suggest me an better alternative to do the same.
Thanks In advance.
When you generate the links in the Web page, do not point directly to the UNC, but to a page of your app, with a parameter to identify the file. In this page, use the same impersonation method to open the file and send its content back to the Web browser.

Categories

Resources