Writing File to Web Server - ASP.NET

Writing file to web server - ASP.NET

protected void TestSubmit_ServerClick(object sender, EventArgs e)
{
using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true))
{
_testData.WriteLine(TextBox1.Text); // Write the file.
}
}

Server.MapPath takes a virtual path and returns an absolute one. "~" is used to resolve to the application root.

write data to local text file from live site

The code in question saves the file to the web server it's running on. It's saving on your computer now because your computer is the web server.

When you move the code to a 'real' web server, that's where the files will be saved, instead, with that code. They will not be saved to your personal computer, or any user's computer. Only on the server itself.


If you need the files to be 'sent' to you, you will need to look into a way to do that. EMail, a database, or a private area of the site where you can browse and view the files are your options. (more info on those is beyond the scope of things here)

One additional detail I have to mention: You need to make very sure that public users will have no access to the uploaded files. If you are running on a server with ASP.NET (including MVC), the App_Data folder is a good place, since by default, web requests cannot be made for files there. You'd then create code to view them separately. (Again; details on this are beyond the scope of this)

File reading and writing inside asp.net web application

Your best bet would be Server.MapPath().

Example:

Put the files inside folder "file" (you can make a folder in your solution, by right clicking your solution and choose add folder)..
then right click the folder..choose existing item , and then choose your files..

To make the path to your files local.. use the follow

Server.MapPath("~\\files\\test.html");

Your code modified

 FileStream file = new FileStream(  Server.MapPath("~\\files\\test.html"), FileMode.Create, FileAccess.Write);

Creating a file on network location in asp.net

All you need to provide is the credentials for that network. That means you need to provide username and password of the network where you are going to store your files on it.


In your function add:

#region network connection
string networkPath = \\192.168.10.19\e;
string userName = @"anil";
string password = "1234";
NetworkCredential credentials = new NetworkCredential(userName, password);
string myNetworkPath = string.Empty;
using (new ConnectToSharedFolder(networkPath, credentials))
{
//your stuff here
// your new path must include: networkpath+ [your folder]
}
#endregion

Create separate controller to make connection in network using above credentials

 public class ConnectToSharedFolder : IDisposable
{
readonly string _networkName;

public ConnectToSharedFolder(string networkName, NetworkCredential credentials)
{
_networkName = networkName;

var netResource = new NetResource
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};

var userName = string.IsNullOrEmpty(credentials.Domain)
? credentials.UserName
: string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);

var result = WNetAddConnection2(
netResource,
credentials.Password,
userName,
0);

if (result != 0)
{
throw new Win32Exception(result, "Error connecting to remote share");
}
}

~ConnectToSharedFolder()
{
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}

[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);

[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags,
bool force);

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}

public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};

public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}

public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
}

How to allow writing to a file on the server ASP.net

You need to give the Network Service account modify rights.
Right click on the folder, choose properties, go to the Security tab and add the Network Service account if it's not there. If it is listed, ensure it had "Modify" checked.

Asp.net writing server side file

Just saw your answer.

It doesn't need to be inside your inetpub or wwwroot directory for that matter, it could be anywhere, as long as security permissions are set correctly for the user under which the application is running as.

But this is actually desired. If not just imagine the consequences of allowing write access anywhere.

Also, there's no need for the virtual directory. You could create a directory like C:\ProcessOutput, and grant permissions accordingly and it should work just fine.
Another option, would be to have a service account created, and impersonate as that user within your application only for when you need to write that output file.



Related Topics



Leave a reply



Submit