Set path to GeoIpDB in asp.net application - c#

I'm having trouble setting path to GeoIp database in my asp.net application.
I have db file in my application root folder, but when i call
using (var reader = new DatabaseReader("GeoLite2-City.mmdb"))
exception "file not found" is thrown.
When i pass full local path #"d:\documents\vsprojects\app\GeoLite2-City.mmdb" to DatabaseReader, application is working. Is there a way to fix this, because this would not work when i upload app to host?
Edit: When i try to access file directly, with http://localhost:45500/GeoLite2-City.mmdb i get an 404.3 error.

Related

Application can find 'Downloads' folder when debugging but not on IIS

I have an application that allows the user to upload a file (saving it to in a folder located in the wwwroot of the ASPNETCORE application). From here they can make edits to it and then they can choose to export the file as a csv/ xml/ xlsx which downloads the file to the user's 'downloads' folder.
While debugging in Visual Studio this all works fine however when I publish and deploy the application to IIS I am getting the exception
Error saving file C:\windows\system32\config\systemprofile\Downloads(FILE NAME)
Could not find part of the path C:\windows\system32\config\systemprofile\Downloads(FILE NAME)
This is the current way I am getting the downloads folder:
FileInfo file = new FileInfo(Path.Combine(Environment.ExpandEnvironmentVariables(#"%USERPROFILE%\Downloads"), data.Filename + "." + data.FileType));
However I have also tried the solution that Hans Passant has answered to a similar question here. Both solutions worjk fine while debugging locally however as soon as I publish them, this one produces the exception:
Value cannot be null. Parameter name: path1
Which I presume is thrown at this point here when I try and save the file to the user's download folder.
using (var package = new ExcelPackage(file))
{
var workSheet = package.Workbook.Worksheets.Add("ExportSheet");
workSheet.Cells.LoadFromCollection(exports, true);
package.Save();
}
I don't really know how I would be able to reproduce these exceptions seeing as locally using Visual Studio it all works fine.
Has anyone else came across this issue while trying to download a file?
UPDATE: When the application is running on IIS, it seems to be using that as the user profile instead of the actually user, so when it tries to navigate to the Downloads folder, it cannot find it. How can I force it to use the user's profile?
LoadUserProfile is already set to True.
Web applications have no knowledge of the end-user's computer's filesystem!
So using Environment.GetFolderPath or Environment.ExpandEnvironmentVariables in server side code will only reveal the server-side user (i.e. the Windows Service Identity)'s profile directories which is completely separate and distinct from your web-application's actual browser-based users OS user profile.
As a simple thought-experiment: consider a user running a weird alien web-browser on an even more alien operating system (say, iBrowse for the Amiga!) - the concept of a Windows-shell "Downloads" directory just doesn't exist, and yet here they are, browsing your website. What do you expect your code would do in this situation?
To "download" a file to a user, your server-side web-application should serve the raw bytes of the generated file (e.g. using HttpResponse.TransmitFile) with the Content-Disposition: header to provide a hint to the user's browser that they should save the file rather than try to open it in the browser.

Printing Telerik reports using ReportProcessor class

Firstly let me describe my setup.
I have a REST Telerik Reporting service. The service operates with TRDX file (have a requirement to have reports in this template format). I also have a console application, which is periodically started by a windows service. This console application uses ReportProcessor class to print reports directly to a printer.
Now the problem.
ReportProcessor throws an exception when trying to access the reports using UriReportSource, when these report files are hosted in IIS together with the REST Reporting service. Browsing and Anonymous Authentication are enabled on IIS for the Reports folder. The error that's thrown is:
The remote server returned an error: (404) not found.
However reports are printed when TRDX files are located in the same folder as the console application.
Solved my own question by trial and error.
My reports were located in the REST service's subdirectory. So I was trying to set the Uri to the reports URL something like this:
UriReportSource reportSource = new UriReportSource {
Uri = "http://localhost/myTelerikRESTService/Reports/samplereport.trdx"
};
And I was obviously getting 404 error since the request to the that path would be handled by the service.
Trying to specify a physical path (c:\inetpub\wwwroot\myTelerikService\Reports\samplereport.trdx) also ended up in error: something like the path format is not supported.
Finally I've tried this Uri: /inetpub/wwwroot/myTelerikService/Reports/samplereport.trdx, that maps to the physical location.
And it worked!
EDIT:
I really should've read this https://www.w3.org/Addressing/URL/uri-spec.html before working on this issue. Apparently I had no firm understanding of what a URI is! Feeling really dumb now.....

Upload File from Client to Server C#

Got an issue "Path Not Found" when try to upload an image from Client site to another server(Not the application server). The code is just like below:
FileUpload1.SaveAs(Server.MapPath(#"xx.xx.xx.xx\" + FileUpload1.FileName));
The error occurred after i have deployed the application into the web application server. But when run locally successfully uploaded.
I suspect the File Uploader that run from the application server cannot found the path from client site. It is because the path of the image does not existed in the application server.
There are two question regarding this issue:
1) Is there anyway to resolved this issue.
2) Is there any differences between "FileUpload1.PostedFile.SaveAs()" and FileUpload1.SaveAs()?
Thanks for trying to help me on this.
You should write path as img.Save("c:\\Users\\user\\Desktop\\Barcodes\\1.png", System.Drawing.Imaging.ImageFormat.Png);

Error: The name server does not exist in current context

I am using windows application to send mail with the attachment. But at this line of code I get error as
The name 'server' does not exist in current context
var directory = Server.MapPath("Attachment/");
I tried from here but it was not working for me
You are trying to use a Server object in a windows application, the Server object is for web applications only.
You can simply specify the full local path to the directory and use this, unlike having to map it based on a virtual directory as in a web application.
If the path to your files/directory are contained in the same folder as your application, you can use Application.StartupPath property and combine this with your own values to get a full path.
var directory = Application.StartupPath + "\\Attachment\\";

asp.net MVC3 create folder where application can write in

I need to create a folder to use for storing files within it, in a .Net MVC3 application, but I think the problem is common to all ASP.Net platform.
Problem is I can create the folder, but cannot write the files, because System.UnauthorizedAccessException occurred.
I also tryed givin extra permission to the user currently running the web app, but nothing changes.
This is my code so far:
if (!System.IO.Directory.Exists(fullPath))
{
System.IO.Directory.CreateDirectory(fullPath);
var user = System.Security.Principal.WindowsIdentity.GetCurrent().User;
var userName = user.Translate(typeof(System.Security.Principal.NTAccount));
var dirInfo = new System.IO.DirectoryInfo(fullPath);
var sec = dirInfo.GetAccessControl();
sec.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(userName,
System.Security.AccessControl.FileSystemRights.Modify,
System.Security.AccessControl.AccessControlType.Allow)
);
dirInfo.SetAccessControl(sec);
System.IO.Directory.CreateDirectory(fullPath);
}
string fullPathFileName = System.IO.Path.Combine(fullPath, fileName);
System.IO.File.WriteAllBytes(fullPath, viaggio.Depliant.RawFile);
Too bad, last line of code always throw System.UnauthorizedAccessException.
I'm not impersonating user in my app, everything run under a predefined user.
What should I do to create a folder and assure that the application can also create files within it?
Edited:
I also tryed to save the files in the App_Data special folder, but I still got the System.UnauthorizedAccessException error. Somebody can tell me why is that happening?
I hate to answer my own question when the problem is that stupid...
I'm just trying to save a file without a proper filename: you can see I'm using the fullPath variable both for creating the folder and for saving the file, instead of using the correctly created fullPathFileName.
Blame on me!
Use App_Data folder, quote from http://msdn.microsoft.com/en-us/library/06t2w7da%28v=vs.80%29.aspx :
To improve the security of the data used by your ASP.NET application, a new subfolder named App_Data has been added for ASP.NET applications. Files stored in the App_Data folder are not returned in response to direct HTTP requests, which makes the App_Data folder the recommended location for data stored with your application, including .mdf (SQL Server Express Edition), .mdb (Microsoft Access), or XML files. Note that when using the App_Data folder to store your application data, the identity of your application has read and write permissions to the App_Data folder.

Categories