2

我正在编写一个小型 Web 应用程序,部分功能是能够将日志从服务器下载到用户的文件系统。我能够压缩现有日志,但无法让压缩文件夹传输。我在这里浏览了许多其他类似的问题,但还没有根据其中任何一个问题来解决这个问题。

这是我目前正在尝试的代码:

.Net 控制器

[HttpPost]
public ActionResult DownloadLogs()
{
    string path = System.Configuration.ConfigurationManager.AppSettings["LogPath"];

    try
    {

        Log.Information("Closing current logger. AudtiLog: {auditLog}", true);
        Log.CloseAndFlush();
        string startPath = path;
        string zipPath = "C:\\my_folder\\result.zip";
        string extractPath = path + "extract";

        //deleting existing zips
        if (System.IO.File.Exists(zipPath))
            System.IO.File.Delete(zipPath);
        if (System.IO.Directory.Exists(extractPath)) 
            System.IO.Directory.Delete(extractPath, true);

        ZipFile.CreateFromDirectory(startPath, zipPath);
        ZipFile.ExtractToDirectory(zipPath, extractPath);

        FileInfo file = new FileInfo(zipPath);

        using (FileStream filestream = new FileStream(zipPath, FileMode.Open))
        {
            return File(filestream, "application/zip", "ServerLogZip.zip");
        }

    }
    catch (Exception ex)
    {
        Log.Error("Error occurred when downloading server logs. Error: {Error}; AuditLog: {auditLog}", ex.Message, true);
        return Json(new { result = "error" });
    }

}

Javascript

function DownloadLogs() {

            $.ajax({
                type: "POST",
                url: "/ManageServer/DownloadLogs",
                contentType: "application/zip",
                success: function (response) {
                    alert("Success")
                },
                error: function (response) {
                    alert("Error");
                }
            });
        }

每当我运行它时,它都会将日志压缩到一个文件夹中,Response成功地逐步完成部分代码,但没有任何反应。我已经尝试调试并单步执行代码,但还没有找到答案。我也试过这个Response.WriteFile方法。没运气。

编辑 我已经更新了要返回的代码ActionResult并返回了一个File. 它当前从服务器返回 500 错误。

4

1 回答 1

1

正如@mason 注意到的那样,您的代码有问题。您正在尝试返回两件事,文件和状态。

应通过 HTTP 返回码检查操作状态。

您可以将IActionResult接口用作返回类型,以便正确处理事情。将方法 aFile作为返回类型,如果一切正常,它将返回您的文件以及所需的所有标题。如果出现问题,您可以返回一个BadRequest("Error Message");

返回类型接受 FileStream 作为参数,其中File包含您的原始数据、文件的 Mime 类型和文件名

为此,请执行以下步骤

  • 将方法返回类型更改为FileResult
  • 创建一个变量,将您的文件内容作为流接收或使用using语句
  • 创建一个byte数组来分配文件内容(如果你试图返回文件流,你会得到一个文件关闭的错误,因为在 retun 发生之前 using 语句已关闭)
  • 像这样返回数据return File(byteArray, "application/zip", "ServerLogZip.zip");

样本

try{
    // Do things to prepare your file

    using(FileStream filestream = new FileStream(zipPath,FileMode.Open))
    {
        byte[] zipBytes= new byte[filestream.Length];
        filestream.Read(PhotoBytes, 0, PhotoBytes.Length);
        return File(zipBytes, "application/zip", "ServerLogZip.zip"); 
    }
}
catch(Exception ex){
    return BadRequest("Something gone wrong: " + ex.Message);
}

我一直在思考如何通过异步请求下载这个文件,但最终,我意识到也许你不需要这么复杂的解决方案。您只需调用路由即可下载文件。

function DownloadLogs() {
    document.location = your_route;
}

为了正常工作,您还必须将 C# 方法的方法装饰器从[HttpPost]更改为[HttpGet]

于 2018-07-17T20:53:01.243 回答