Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# save files to folder on server instead of local

The project is an MVC 4 C# based web application.

I'm currently working locally and would like to have the ability to upload a file to my locally running application in debug mode and have the file saved to the web server instead of the my local folder.

Currently we are using:

if (!System.IO.File.Exists(Server.MapPath(PicturePath)))
{
     file.SaveAs(Server.MapPath(PicturePath));
}

How can we leverage this code to save to a webserver directly. Is that even possible?

Right now the file gets saved to my local path and the path is stored in the database, we then have to upload the files to the webserver manually.

like image 366
Rob Carroll Avatar asked Jul 24 '13 15:07

Rob Carroll


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

The SaveAs function takes a filename and will save the file to any path you give it, providing the following conditions are met:

  • Your local machine can access the filepath
  • Your local account has the correct privileges to write to the filepath

I would suggest you have a web.config setting that can be checked when running your code. Then you can decide if to use Server.MapPath or an absolute path instead.

For example, in "debug" mode (running locally) you might have the following settings:

<appSettings>
    <add key="RunningLocal" value="true" />
    <add key="ServerFilePath" value="\\\\MyServer\\SomePath\\" />
</appSettings>

and in "live" mode:

<appSettings>
    <add key="RunningLocal" value="false" />
    <add key="ServerFilePath" value="NOT USED" />
</appSettings>

Then your code may look something like this:

bool runningLocal = GetFromConfig();
bool serverFilePath = GetFromConfig();
string filePath;

if(runningLocal)
    filePath = serverFilePath;
else
    filePath = Server.MapPath(PicturePath);

if (!System.IO.File.Exists(filePath ))
{
     file.SaveAs(filePath );
}
like image 71
musefan Avatar answered Sep 18 '22 18:09

musefan