如何获得用户上传文件的物理路径? [英] How do I get a physical path where the user upload a file?

查看:300
本文介绍了如何获得用户上传文件的物理路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Web的应用程序,该应用程序用户可以登录后登录,它将上传文件.但是我无法将文件保存在服务器上,我只是读取数据,而数据将如何保存在数据库中文件退出的用户目录路径.

我尝试过的事情:

字符串路径= HttpContext.Current.Server.MapPath(");

i have a web based application in that application user can login after login it will upload the files.but i can not save the file on my server i just read the data and data will be saved in the database how i get the user directory path where the file exits.

What I have tried:

string path = HttpContext.Current.Server.MapPath("");

推荐答案

用户上载文件时,它不会进入任何目录.它仍然位于Files集合中的Request对象中.由您来获取内容并将文件保存在适当的位置.

参见 [
When a user uploads a file it doesn''t go to any directory. It''s still sitting in the Request object, in the Files collection. It''s up to you to get the content and save the files somewhere appropriate.

See this[^] for an example.


我看到您正在处理MVC6 (问题标记中所示). Server.MapPath可能不起作用.您需要处理IApplicationEnvironment接口,名称空间Microsoft.Extensions.PlatformAbstractions来获取服务器路径.像
I see you are dealing with MVC6 (as it shown in the question tag). Server.MapPath may not work. You need to deal with IApplicationEnvironment interface, namespace Microsoft.Extensions.PlatformAbstractions to get the server path. Something like
private readonly IApplicationEnvironment _appEnvironment;
public HomeController(IApplicationEnvironment appEnvironment)
{
    _appEnvironment = appEnvironment;
}

public IActionResult Index()
{
    var rootPath = _appEnvironment.ApplicationBasePath;
    return View();
}


欲了解更多信息,请点击这里.
ASP.NET Core Apps中的环境变量 [


For more look here. Environment Variables in ASP.NET Core Apps[^]


这篇关于如何获得用户上传文件的物理路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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