C#ASP.NET MVC-将XML下载到浏览器

我正在使用ASP.NET 4.6 MVC。我将XML文件另存为字节数组在我的SQL数据库中。我想允许我的用户从浏览器下载文件。我没有任何错误,也没有文件下载。我知道有很多这样的问题,但是我还没有看到没有下载文件的地方(甚至没有损坏的地方)。

public actionResult DownloadScript(int id) {
    try {
        var script = _db.PortalScripts.FirstOrDefault(i = >i.Id == id);
        if (script != null) {
            return File(script.ScriptBytes,"text/xml",script.Name);
        }
    }
    catch(Exception e) {
        flashMessage.Danger("Error downloading script");
    }

    return RedirectToaction("Scripts");
}
    [HttpPost]
    public actionResult UploadScript(httppostedfileBase file) {
        try {
            if (file.ContentLength > 0) {
                var newScript = new PortalScript {
                    Name = Path.GetFileName(file.FileName),Version = "190.161",Description = "This is a placeholder",Displayed = true
                };

                using(MemoryStream ms = new MemoryStream()) {
                    file.InputStream.CopyTo(ms);
                    newScript.ScriptBytes = ms.ToArray();
                }

                var existingScripts = _db.PortalScripts.FirstOrDefault(s = >s.ScriptBytes == newScript.ScriptBytes);

                if (existingScripts == null) {
                    _db.PortalScripts.AddOrupdate(newScript);
                    _db.SaveChanges();
                    flashMessage.Confirmation(newScript.Name + " successfully uploaded");
                }
                else flashMessage.Warning(newScript.Name + " already exists. Duplicate upload ignored.");

            }
        }
        catch(Exception ex) {
            flashMessage.Danger("Upload failed: " + ex.Message);
        }

        return RedirectToaction("Scripts");
    }

在我的下载方法中,我的脚本变量返回了正确填写的模型。也不会陷入困境。只是重定向回我的原始视图而没有文件。有什么建议吗?

hia123 回答:C#ASP.NET MVC-将XML下载到浏览器

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3121100.html

大家都在问