如何从服务器下载文件 [英] How to download files from the server
问题描述
嗨朋友们,
我有一个大问题,我正在努力解决这个问题。
我想从服务器下载文件。我使用过asp.net mvc 4解决方案。
我的意见代码如下。
< pre lang =Javascript> $。ajax({
url:' @ Url.Action(DownloadFile ,收件箱)',
类型:' POST',
cache: false ,
data:{
filePaths:filePaths
},
成功: function (result){
stopPreLoader();
},
错误: function (result){
stopPreLoader();
}
});
这里filePaths是文件名列表。我的控制器如下所示,
[HttpPost]
public FileResult DownloadFile( string filePaths)
{
var pathList = filePaths .Split(' ,')。其中(p = > p!= )。ToList();
var path = @ + pathList [ 0 ];
if (pathList.Count == 1 )
{
byte [] fileBytes = System.IO.File.ReadAllBytes(path);
string fileName = abc.pdf 跨度>;
return 文件(fileBytes,System.Net.Mime.MediaTypeNames.Application.Zip,fileName);
}
if (pathList.Count > 1 )
{
var id = Session.SessionID;
var archive = Server.MapPath( 〜/ archive.zip跨度>);
var temp = Server.MapPath( 〜/ TEMP跨度>);
// 清除所有现有存档
if (System.IO.File.Exists(archive))
{
System.IO.File.Delete(archive);
}
// 清空临时文件夹
Directory.EnumerateFiles (temp)。ToList()。ForEach(f = > System.IO.File.Delete(f));
// 将所选文件复制到临时文件夹
pathList.ForEach(f = > System.IO.File.Copy(f,Path.Combine(temp,Path.GetFileName(f))));
// 创建新档案
ZipFile.CreateFromDirectory( temp,archive);
return 文件(存档, application / zip, archive.zip);
// 返回文件(存档,MimeMapping.GetMimeMapping(存档));
}
return null ;
}
因此,在服务器中创建archive.zip文件。但我在浏览器上没有出现下载确认框。
请帮我解决这个问题。
我尝试过的事情:
我已经在互联网上的每个地方搜索并尝试了一天。
.ajax({
url:' @ Url.Action (DownloadFile,收件箱)',
类型:' POST',
cache: false ,
data:{
filePaths:filePaths
},
成功: function (结果){
stopPreLoader();
},
错误:功能(结果){
stopPreLoader();
}
});
这里filePaths是文件名列表。我的控制器如下所示,
[HttpPost]
public FileResult DownloadFile( string filePaths)
{
var pathList = filePaths .Split(' ,')。其中(p = > p!= )。ToList();
var path = @ + pathList [ 0 ];
if (pathList.Count == 1 )
{
byte [] fileBytes = System.IO.File.ReadAllBytes(path);
string fileName = abc.pdf 跨度>;
return 文件(fileBytes,System.Net.Mime.MediaTypeNames.Application.Zip,fileName);
}
if (pathList.Count > 1 )
{
var id = Session.SessionID;
var archive = Server.MapPath( 〜/ archive.zip跨度>);
var temp = Server.MapPath( 〜/ TEMP跨度>);
// 清除所有现有存档
if (System.IO.File.Exists(archive))
{
System.IO.File.Delete(archive);
}
// 清空临时文件夹
Directory.EnumerateFiles (temp)。ToList()。ForEach(f = > System.IO.File.Delete(f));
// 将所选文件复制到临时文件夹
pathList.ForEach(f = > System.IO.File.Copy(f,Path.Combine(temp,Path.GetFileName(f))));
// 创建新档案
ZipFile.CreateFromDirectory( temp,archive);
return 文件(存档, application / zip, archive.zip);
// 返回文件(存档,MimeMapping.GetMimeMapping(存档));
}
return null ;
}
因此,在服务器中创建archive.zip文件。但我在浏览器上没有出现下载确认框。
请帮我解决这个问题。
我尝试过的事情:
我搜索了互联网上的每一个地方并尝试了一天。
您无法通过ajax执行此操作,只需使用提交到DownloadFile操作的普通表单,浏览器将负责其余操作。如果您不想使用提交按钮,可以通过javascript提交表单。
Hi Friends,
I have a big problem and i am trying for a day to solve this.
I want to down load files from the server. I have used asp.net mvc 4 solution.
My views code is like below.
$.ajax({
url: '@Url.Action("DownloadFile", "Inbox")',
type: 'POST',
cache: false,
data: {
filePaths: filePaths
},
success: function (result) {
stopPreLoader();
},
error: function (result) {
stopPreLoader();
}
});
Here filePaths is list of names of files. Further my controller is like below,
[HttpPost]
public FileResult DownloadFile(string filePaths)
{
var pathList = filePaths.Split(',').Where(p => p != "").ToList();
var path = @"" + pathList[0];
if (pathList.Count == 1)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(path);
string fileName = "abc.pdf";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Zip, fileName);
}
if (pathList.Count > 1)
{
var id = Session.SessionID;
var archive = Server.MapPath("~/archive.zip");
var temp = Server.MapPath("~/temp");
// clear any existing archive
if (System.IO.File.Exists(archive))
{
System.IO.File.Delete(archive);
}
// empty the temp folder
Directory.EnumerateFiles(temp).ToList().ForEach(f => System.IO.File.Delete(f));
// copy the selected files to the temp folder
pathList.ForEach(f => System.IO.File.Copy(f, Path.Combine(temp, Path.GetFileName(f))));
// create a new archive
ZipFile.CreateFromDirectory(temp, archive);
return File(archive, "application/zip", "archive.zip");
//return File(archive, MimeMapping.GetMimeMapping(archive));
}
return null;
}
So above archive.zip file is created in the server. But I does not appear download confirmation box on the browser.
Please help me to solve this issue.
What I have tried:
I have searched every where on the internet and tried for a day.
.ajax({ url: '@Url.Action("DownloadFile", "Inbox")', type: 'POST', cache: false, data: { filePaths: filePaths }, success: function (result) { stopPreLoader(); }, error: function (result) { stopPreLoader(); } });
Here filePaths is list of names of files. Further my controller is like below,
[HttpPost] public FileResult DownloadFile(string filePaths) { var pathList = filePaths.Split(',').Where(p => p != "").ToList(); var path = @"" + pathList[0]; if (pathList.Count == 1) { byte[] fileBytes = System.IO.File.ReadAllBytes(path); string fileName = "abc.pdf"; return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Zip, fileName); } if (pathList.Count > 1) { var id = Session.SessionID; var archive = Server.MapPath("~/archive.zip"); var temp = Server.MapPath("~/temp"); // clear any existing archive if (System.IO.File.Exists(archive)) { System.IO.File.Delete(archive); } // empty the temp folder Directory.EnumerateFiles(temp).ToList().ForEach(f => System.IO.File.Delete(f)); // copy the selected files to the temp folder pathList.ForEach(f => System.IO.File.Copy(f, Path.Combine(temp, Path.GetFileName(f)))); // create a new archive ZipFile.CreateFromDirectory(temp, archive); return File(archive, "application/zip", "archive.zip"); //return File(archive, MimeMapping.GetMimeMapping(archive)); } return null; }
So above archive.zip file is created in the server. But I does not appear download confirmation box on the browser.
Please help me to solve this issue.
What I have tried:
I have searched every where on the internet and tried for a day.
You can't do this via ajax, just use a normal form that submits to your DownloadFile action and the browser will take care of the rest. You can submit the form via javascript if you don't want to use a submit button.
这篇关于如何从服务器下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!