使用actionlink下载文件mvc2

时间:2013-08-10 07:32:11

标签: file-upload asp.net-mvc-2

我想使用asp.net mvc2上传和下载简历。我已经创建了编码。它已成功上传。当我尝试下载文件时,我遇到了问题.. 它显示一个空页..

控制器:

 [HandleErrorWithAjaxFilter]
    public ActionResult UploadResume(HttpPostedFileBase FileData)
    {
        Stream fromStream = FileData.InputStream;
        Stream toStream = new FileStream(Server.MapPath("~/Content/Resumes/") + FileData.FileName, FileMode.Create);

        LoggedInCandidate.ResumeFileName = FileData.FileName;
        //_repository.Save();
        _userRepository.Save();

        return Json(new JsonActionResult
        {
            Success = true,
            Message = "Resume has been uploaded."
        });
        //return Json("Resume has been uploaded.");
    }

查看:

    <input id="Resume" type="file" name="Resume" />     

下载:

<p>
                <% var link = Url.Content("~/Content/Resumes/") + Model.ResumeFileName; %>
                <a href="<%: link %>">Download Resume</a> 
            </p>

当我点击链接下载简历时,它会在网址上显示该文件的名称,但不会下载。

2 个答案:

答案 0 :(得分:0)

这是你应该怎么做的。在Controller中创建一个Action,如下所示:

public FileResult Download(string fileName)
{
    var path = Path.Combine(Server.MapPath("~/Content/Resumes/"), fileName);
    var fileStream = new FileStream(path, FileMode.Open);

    // Assuming that the resume is an MS Word document...
    return File(fileStream, "application/vnd.ms-word");
}

而且,在您的视图中,您将拥有:

<p>
  <%: Html.ActionLink("Download Resume", "Download", new { fileName = Model.ResumeFileName }) %>
</p>

答案 1 :(得分:-1)

<强>控制器:

 [Authorize]
    public ActionResult Download(string fileName)
    {
        string pfn = Server.MapPath("~/Content/Resumes/" + fileName);

        if (!System.IO.File.Exists(pfn))
        {
            //throw new ArgumentException("Invalid file name or file not exists!");

            return Json(new JsonActionResult { Success = false, Message = "Invalid file name or file not exists!" });
        }
        else
        {

            return new BinaryContentResult()
            {
                FileName = fileName,
                ContentType = "application/octet-stream",
                Content = System.IO.File.ReadAllBytes(pfn)
            };
        }

    }

<强>型号: / BinaryFileResult

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;

命名空间Dial4Jobz.Models {     public class BinaryContentResult:ActionResult     {

    public BinaryContentResult()
    {
    }

    public string ContentType { get; set; }
    public string FileName { get; set; }
    public byte[] Content { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {

        context.HttpContext.Response.ClearContent();
        context.HttpContext.Response.ContentType = ContentType;

        context.HttpContext.Response.AddHeader("content-disposition",

        "attachment; filename=" + FileName);

        context.HttpContext.Response.BinaryWrite(Content);
        context.HttpContext.Response.End();
    }
}

}