C#将文件保存到服务器上的文件夹的地方,而不是 [英] C# save files to folder on server instead of local

查看:2963
本文介绍了C#将文件保存到服务器上的文件夹的地方,而不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该项目是一个MVC 4基于C#的Web应用程序。

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

我目前在本地工作,希望有在调试模式下的文件上传到我的本地运行应用程序并保存到Web服务器,而不是我的本地文件夹中的文件的能力。

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.

目前我们使用的是:

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

我们如何利用这个code保存到直接在Web服务器。是,即使可能吗?

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.

推荐答案

另存为函数接受一个文件名,将文件保存到你给它的任何路径,提供了满足以下条件:

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


  • 您的本地机器可以访问文件路径

  • 您的本地帐户具有正确的权限写入到文件路径

我建议你可以运行你的code时,被检查一个web.config设置。然后你可以决定是否使用使用Server.Mappath 或绝对路径来代替。

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" more (running locally) you might have the following settings:

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

在现场模式:

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

那么你的code可能是这个样子:

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 );
}

这篇关于C#将文件保存到服务器上的文件夹的地方,而不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆