Mvc - 下载文件 - 文件路径问题 [英] Mvc - downloading files - file path issues

查看:416
本文介绍了Mvc - 下载文件 - 文件路径问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



请原谅我缺乏知识,我是初学者。



我有一个允许用户上传和下载文件的应用程序。我可以将文件上传到远程服务器并显示该文件夹的内容没问题。当我尝试下载时出现问题,特别是当我尝试创建正确的路径时。这是我的下载代码:



我尝试过:



Hi everyone,

Please excuse my lack of knowledge, I am a beginner at best.

I have an application that allows users to upload and download files. I can upload the file to a remote server and display contents of the folder no problem. My problem arises when I try to download, in particular when I try create the correct path. Here is my download code:

What I have tried:

<pre>  public FileResult Download(string ImageName)
        {
            var IdSes = Session["IdSes"];
           string FileVirtualPath= "/Da-app01/Data/Tracker/uploads/" + IdSes +"/"+ ImageName;

            return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
        }







当我尝试下载文件时出现此错误:




When I try download a file I get this error:

[DirectoryNotFoundException: Could not find a part of the path 'C:\Users\G\Desktop\application\Da-app01\Data\Tracker\uploads\33\test1.pdf'.]





问题是我需要的路径是



The problem is the path I need is

引用:

Da-app01 \Data \Tracker \ uploads \\\ test1.pdf

Da-app01\Data\Tracker\uploads\33\test1.pdf







Where does

Quote:

C:\ Users\G\Desktop\application

C:\Users\G\Desktop\application

来自哪里?我可以以某种方式将其删除吗?



在这里苦苦挣扎!任何理解的帮助将不胜感激!

come from? Can I remove it somehow?

Struggling Here! Any help to understand would be appreciated!

推荐答案

在MVC应用程序眼中,您的变量 FileVirtualPath 是一个相对路径,所以你的应用程序显然位于c:\users \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b $ b为了正确下载文件,如果文件本地存储在您的计算机上,则必须使用文件所在位置的完整路径。因此,您的文件不在您的计算机本地,位于 Da-app-01 \ Data \Tracker \ uploads \\\ test1.pdf 它必须包含驱动器路径。



您的文件需要引用类似的字符串路径= HttpContext.Current.Server.MapPath(〜/+控制器操作中的FileVirtualPath);



您还可以选择 HostingEnvironment.MapPath(〜/ + FileVirtualPath) Server.MapPath(〜/+ FileVirtualPath)
Your variable FileVirtualPath, in the eyes of MVC application, is a relative path so your application is apparently in c:\users\g\desktop\application directory so it is concatinating your file location with where it is locally.

In order for the file to download correctly, if it is stored locally on your computer, it has to utilize the full path of where the file is located. So your file isn't located locally on your computer at Da-app-01\Data\Tracker\uploads\33\test1.pdf it has to include the drive path.

Your file needs to reference something like String path = HttpContext.Current.Server.MapPath("~/" + FileVirtualPath); within your controllers action.

You also have options of HostingEnvironment.MapPath("~/" + FileVirtualPath) or Server.MapPath("~/" + FileVirtualPath)


这篇关于Mvc - 下载文件 - 文件路径问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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