Skip to content

Commit 4472d5e

Browse files
committed
Improved methods to download import or export files
1 parent c2b7103 commit 4472d5e

File tree

2 files changed

+63
-27
lines changed

2 files changed

+63
-27
lines changed

src/Presentation/SmartStore.Web/Administration/Controllers/ExportController.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Net.Mime;
6+
using System.Text;
67
using System.Web;
78
using System.Web.Mvc;
89
using SmartStore.Admin.Extensions;
@@ -1057,19 +1058,25 @@ public ActionResult DownloadLogFile(int id)
10571058
return AccessDeniedView();
10581059

10591060
var profile = _exportService.GetExportProfileById(id);
1060-
if (profile == null)
1061-
return RedirectToAction("List");
1062-
1063-
var path = profile.GetExportLogPath();
1064-
var stream = new FileStream(path, FileMode.Open);
1061+
if (profile != null)
1062+
{
1063+
var path = profile.GetExportLogPath();
1064+
if (System.IO.File.Exists(path))
1065+
{
1066+
var stream = new FileStream(path, FileMode.Open);
1067+
var result = new FileStreamResult(stream, MediaTypeNames.Text.Plain);
10651068

1066-
var result = new FileStreamResult(stream, MediaTypeNames.Text.Plain);
1069+
return result;
1070+
}
1071+
}
10671072

1068-
return result;
1073+
return RedirectToAction("List");
10691074
}
10701075

10711076
public ActionResult DownloadExportFile(int id, string name)
10721077
{
1078+
string message = null;
1079+
10731080
if (_services.Permissions.Authorize(StandardPermissionProvider.ManageExports))
10741081
{
10751082
var profile = _exportService.GetExportProfileById(id);
@@ -1091,8 +1098,17 @@ public ActionResult DownloadExportFile(int id, string name)
10911098
}
10921099
}
10931100
}
1101+
else
1102+
{
1103+
message = T("Admin.AccessDenied.Description");
1104+
}
1105+
1106+
if (message.IsEmpty())
1107+
{
1108+
message = T("Admin.Common.ResourceNotFound");
1109+
}
10941110

1095-
return new EmptyResult(); // TODO
1111+
return File(Encoding.UTF8.GetBytes(message), MediaTypeNames.Text.Plain, "DownloadExportFile.txt");
10961112
}
10971113

10981114
public ActionResult ResolveFileNamePatternExample(int id, string pattern)

src/Presentation/SmartStore.Web/Administration/Controllers/ImportController.cs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using System.Net.Mime;
6+
using System.Text;
67
using System.Web.Mvc;
78
using SmartStore.Admin.Extensions;
89
using SmartStore.Admin.Models.DataExchange;
@@ -633,37 +634,56 @@ public ActionResult DownloadLogFile(int id)
633634
return AccessDeniedView();
634635

635636
var profile = _importService.GetImportProfileById(id);
636-
if (profile == null)
637-
return RedirectToAction("List");
638-
639-
var path = profile.GetImportLogPath();
640-
var stream = new FileStream(path, FileMode.Open);
637+
if (profile != null)
638+
{
639+
var path = profile.GetImportLogPath();
640+
if (System.IO.File.Exists(path))
641+
{
642+
var stream = new FileStream(path, FileMode.Open);
643+
var result = new FileStreamResult(stream, MediaTypeNames.Text.Plain);
641644

642-
var result = new FileStreamResult(stream, MediaTypeNames.Text.Plain);
645+
return result;
646+
}
647+
}
643648

644-
return result;
649+
return RedirectToAction("List");
645650
}
646651

647652
public ActionResult DownloadImportFile(int id, string name)
648653
{
649-
if (!_services.Permissions.Authorize(StandardPermissionProvider.ManageImports))
650-
return AccessDeniedView();
654+
string message = null;
651655

652-
var profile = _importService.GetImportProfileById(id);
653-
if (profile == null)
654-
return RedirectToAction("List");
656+
if (_services.Permissions.Authorize(StandardPermissionProvider.ManageImports))
657+
{
658+
var profile = _importService.GetImportProfileById(id);
659+
if (profile != null)
660+
{
661+
var path = Path.Combine(profile.GetImportFolder(true), name);
655662

656-
var path = Path.Combine(profile.GetImportFolder(true), name);
663+
if (!System.IO.File.Exists(path))
664+
path = Path.Combine(profile.GetImportFolder(false), name);
657665

658-
if (!System.IO.File.Exists(path))
659-
path = Path.Combine(profile.GetImportFolder(false), name);
666+
if (System.IO.File.Exists(path))
667+
{
668+
var stream = new FileStream(path, FileMode.Open);
669+
var result = new FileStreamResult(stream, MimeTypes.MapNameToMimeType(path));
670+
result.FileDownloadName = Path.GetFileName(path);
660671

661-
var stream = new FileStream(path, FileMode.Open);
672+
return result;
673+
}
674+
}
675+
}
676+
else
677+
{
678+
message = T("Admin.AccessDenied.Description");
679+
}
662680

663-
var result = new FileStreamResult(stream, MimeTypes.MapNameToMimeType(path));
664-
result.FileDownloadName = Path.GetFileName(path);
681+
if (message.IsEmpty())
682+
{
683+
message = T("Admin.Common.ResourceNotFound");
684+
}
665685

666-
return result;
686+
return File(Encoding.UTF8.GetBytes(message), MediaTypeNames.Text.Plain, "DownloadImportFile.txt");
667687
}
668688

669689
[HttpPost]

0 commit comments

Comments
 (0)