1

我想使用 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>

当我单击链接下载简历时,它会在 url 处显示文件的名称,但它没有下载。

4

2 回答 2

0

这是你应该如何做的。在控制器中创建一个动作,如下所示:

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>
于 2013-08-14T20:34:25.413 回答
-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 { 公共类 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();
    }
}

}

于 2013-08-22T06:57:28.630 回答