I have created a web service which gives the data in JSON format, I am going to read this data to create a high-charts. When I coded this web service , I get values in string which I have serialized into JSON format and store it my folder on system, but problem is when I will deploy my web service in remote machine that time I will face the problem to store the file since I have provided local path as explained in below code,
System.IO.File.WriteAllText(#"C:\Json\Json.json", jSearializer.Serialize(modified_listofstrings));
Can anyone please suggest what I am suppose to do so that I can store this file in such a way that it will be easy to access after deployment of my web service in remote machine?
Is that possible or I will have to create a simple asp.net application and consume that web service and store that file in the folder of that newly created application?
I am very new to this concept hence I don't know about storing in virtual folder or something like that, I got suggestion to do so, It will be very grateful if someone explains me the concept as well...
You have two ways:
Give write permission to your application identity to write in c:\json folder, which is not a good idea.
Change your code to use relative path:
File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath("/json"), "json-text");
Server.MapPath maps a virtual directory to it's equivalent absolute directory in OS. For example, if your website is hosted in c:\websites\json-project\, then using Server.MapPath("/foo") would be translated to c:\websites\json-project\foo path.
By default any ASP.NET application has full access to all of its folders.
You can find the physical path to your application by giving relative path something like Server.MapPath("/Json/Json.json")
for more information check below SO question
Server.MapPath("."), Server.MapPath("~"), Server.MapPath(#"\"), Server.MapPath("/"). What is the difference?
Related
I've a .Net/C# Windows application which can create text/pdf files. My requirement is to make these files accessible via http links for another internal application inside my organization. The internal application only accepts http links (ftp and others are not supported).
I know this can be done manually by placing these files in an IIS server. I would like to know whether there is any easy way to do this programmatically? Once the file is created locally in my application, I should have a http link to access the file.
I'm not sure if I understand your need correctly. You can point it out when anything wrong.
Assuming that the application create pdf files in a folder named PDFile. On IIS, you can add a site and set it physical path to PDFile folder. If you enable Directory Browsing, you can see all files when enter the url of this site in browser like this:
When you want to access these file from an external machine, just enter the url http://serverIP:port/PDF file name for example http://xxx.xxx.xxx.xxx:80/mycustomefile.pdf. No need to set anything programmatically.
I'm having some trouble with reading and writing files on the server with my Web-Application.
I have a c#-class library for my business logic and a ASP.net Web Application as front-end.
The whole application works fine when I start it from Visual Studio.
When I first tried to deploy it I got an error that I don't have permission to write on c:/inetpub/wwwroot/myfolder...
Then I tried to use relative paths like /myfolder but I always get the message cannot access C:/myfolder! And I really don't know where the "C" is coming from?
I would assume that you are trying to write to the file system from your application. In that case the "C" comes from the server. ASP.Net and IIS know where your application resides on the server and are mapping the path from the URL to the file system (C:). It works when you start things from Visual Studio because it's being run under an account that has write permissions to the file system. The IIS Application Pool User that is running on your server does not have write permissions. To resolve this issue you can give that user write permissions. You can see how to do that in this post.
I have a little problem because on my website I can't really set permissions for the directories, it just doesn't work. I use filezilla and when I try to set them to some subdirectories of a default directory of the server that is publicly readable and writable it doesn't send me an error but when I want to save a file in one of that subdirectories from an asp page I get an error telling that I haven't got the permission to do so, I tried setting 777, so it should work. Using C# I created a FTP client that allows me to edit those subdirectories (those I couldn't edit from the asp page). My question is: can I create an asp page that uses a FTP client to access one of those subdirectories????? Thanks!!!
As I mentioned before you could try WCF and stream files to your server using a web service.
WCF could be fairly complex; however, there is plenty of tutorials online. You could start with this simple tutorial and adjust it to accomplish what you need. They even provide source code.
Basically what you need is a web service with one method, say:
void UploadFile(Stream object)
Then you need to create a web reference from your client (windows form app for example) by providing the address of your WCF web service. This will automatically create the classes you need to execute the method.
This site has more info about streaming files:
set permission to the account that your website uses to access any directory.By default in IIS iis_user account is used.Chage your web site user account to ftp user or any account that have the permission to access those directories.You can easily change this permission using iis7 manager.
I need a way to acccess files on a fileshare from a different domain from my own? for example, I have here is an application that exists on a sever in Domain1 and this application needs to retrieve files from a server on Domain2.
Any ideas...
Does this help you in the right direction? I assume it's what your asking...
Map a Network Drive From Code for Cross-Domain File Copy.
The CodeProject link given on the site also gives the source code for downloading.
I recently had a similar issue and executing
net use \\machine.otherdomain myusername /USER:password
as part of the user's network logon was a solution for us.
This is obviously not perfect but for our environment it was sufficient.
Ok, my web application is at C:\inetpub\wwwroot\website
The files I want to link to are in S:\someFolder
Can I make a link in the webapp that will direct to the file in someFolder?
If its on a different drive on the server, you will need to make a virtual directory in IIS. You would then link to "/virtdirect/somefolder/"
You would have to specifically map it to some URL through your web server. Otherwise, all your files would be accessible to anyone who guessed their URL and you don't want that...
Do you have another virtual directory/application pointing to s:\someFolder? If so, it's just a simple link.
Are you trying to stream files back? If so, take a look at Response.TransmitFile and Response.WriteFile.
Otherwise, maybe you could create a handler (.ashx) to grab a specified file and stream its contents back?
i think there are only two ways
1) make a virtual path wich points to download directory
2) call a your aspx/ashx handler wich load file locally and send it to client.
A solution which works at the OS level rather than the webserver level is to make a symbolic link.
Links to files are supported on Vista and links to folders ("junctions") are supported on Win2000 onwards.
That depends on the configuration of your web server. Probably not. You don't want the web server to be able to access any file on the hard drive (ie your passwords file), so only those files configured to be accessible in the web server's configuration files are accessible and can be linked to. Usually these are all kept under one directory. You could, of course, copy someFolder and place it under your web directory, then it would be accesible, or if you are sure it is safe, change the configuraton of your web server to allow access to that folder.