如何使用 MVC 以最小访问权限在服务器上记录文件

发布于 2024-11-30 19:33:52 字数 872 浏览 0 评论 0原文

如何使用 MVC 以最小访问权限在服务器上记录文件。该代码的下一个版本不适合,因为它会堵塞域。

控制器代码:

var fileFile = Request.Files["p" + prop.Id];
if (fileFile == null) continue;

string pathFile = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles";
string filenameFile = Path.GetFileName(fileFile.FileName);

if (filenameFile != null) fileFile.SaveAs(Path.Combine(pathFile, filenameFile));

(如果可以通过将文件放入缓存来实现这一点)

编辑代码:

varordinaryPropertyValue = new Catalog.Core。 Entities.OrdinaryPropertyValue();

环境.CurrentDirectory = 环境.GetEnvironmentVariable("TEMP");

var fileFile = Request.Files["File" + prop.Id]; if (fileFile == null) 继续;

字符串路径文件=环境.当前目录;

文件文件.另存为(路径文件);

普通PropertyValue.Value = pathFile;

instance.SetPropertyValue(prop.Id,ordinaryPropertyValue);

How to record files on a server with minimal access using MVC. The next version of the code is not suitable as it will be clogging up the domain.

Controller code:

var fileFile = Request.Files["p" + prop.Id];
if (fileFile == null) continue;

string pathFile = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles";
string filenameFile = Path.GetFileName(fileFile.FileName);

if (filenameFile != null) fileFile.SaveAs(Path.Combine(pathFile, filenameFile));

(if it is possible to realize this by putting file to the cache)

EDITED CODE:

var ordinaryPropertyValue = new Catalog.Core.Entities.OrdinaryPropertyValue();

Environment.CurrentDirectory = Environment.GetEnvironmentVariable("TEMP");

var fileFile = Request.Files["File" + prop.Id];
if (fileFile == null) continue;

string pathFile = Environment.CurrentDirectory;

fileFile.SaveAs(pathFile);

ordinaryPropertyValue.Value = pathFile;

instance.SetPropertyValue(prop.Id, ordinaryPropertyValue);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短叹 2024-12-07 19:33:52

您可以使用通用的应用程序数据位置(如果您需要保留它们,这将比 %TEMP% 更好 - 根据我的经验,人们倾向于不时清理 %TEMP% 文件夹) - 所有用户都应该有访问该文件夹,然后您可以将其放入子文件夹中,如下所示:

var appDataFolder = System.Environment.GetFolderPath(SpecialFolder.CommonApplicationData);
var dir = Path.Combine(Path.Combine(appDataFolder, "My Application"), "UploadedFiles"):

// save your files into dir

以下是所有“特殊文件夹”,以防您看到您更愿意使用的文件夹:Environment.SpecialFolder

You could use the common application data location (this would be better than %TEMP% if you need them to stick around - people have a tendency to clean the %TEMP% folder out from time to time in my experience) - all users should have access to that and then you could just put it in a sub-folder like so:

var appDataFolder = System.Environment.GetFolderPath(SpecialFolder.CommonApplicationData);
var dir = Path.Combine(Path.Combine(appDataFolder, "My Application"), "UploadedFiles"):

// save your files into dir

Here are all the "special folders" in case you see one that you would rather use: Environment.SpecialFolder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文