我试图获取和下载文件从服务器本地文件夹到客户端。但当我尝试下载时(也是下载操作成功),下载文件的名称被自动设置为动作名称。如何更改下载的文件名?
MyController:
[HttpGet]
public PhysicalFileResult MYACTIONDOWNLOAD(string filePathAndName)
{
string downloadPath = Path.Combine(Directory.GetCurrentDirectory(), @"C:\", filePathAndName);
string filesMimeType = MimeTypesMap.GetMimeType(filePathAndName);
return new PhysicalFileResult(downloadPath, filesMimeType);
}
客户端(查看脚本):
<a target="_blank" href="(my website www root url)/MYPROJECTNAME/MYCONTROLLERNAME/MYACTIONDOWNLOAD?filePathAndName=\\192.168.X.X\MYREMOTEDISC-1\MYDOCUMENTS\SCHOOL\10012021_1023350.docx"></a>
当我点击下载操作是okey,但下载的文件名是MyActionDownload.docx,我想更改文件名。
已安装路径使用
Var bytes=file.readallbytes(“您的路径”)
后来呢
返回文件(字节、mimetype、“想要的名称”);
你可以试试这个方法。
[HttpGet]
public async Task<IActionResult> Download(string path)
{
var memory = new MemoryStream();
string webRootPath = _HostEnvironment.WebRootPath;
var uploads = Path.Combine(webRootPath + path);
using (var stream = new FileStream(uploads, FileMode.Open))
{
await stream.CopyToAsync(memory);
}
memory.Position = 0;
var ext = Path.GetExtension(uploads).ToLowerInvariant();
return File(memory, "application/octet-stream", "any file name");
}
_HostEnvironment.webrootPath将获得服务器的绝对路径。如果它来自您的URL,那么您可以避免这种情况。