File upload 使用actionlink下载文件mvc2

File upload 使用actionlink下载文件mvc2,file-upload,asp.net-mvc-2,File Upload,Asp.net Mvc 2,我想用asp.net mvc2上传和下载简历。我已经创建了编码。上传成功。当我试图下载文件时,我遇到了一个问题。。 它显示的是一个空白页 控制器: [HandleErrorWithAjaxFilter] public ActionResult UploadResume(HttpPostedFileBase FileData) { Stream fromStream = FileData.InputStream; Stream toStream =

我想用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>
 [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)
            };
        }

    }
查看:

 [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>
 [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)
            };
        }

    }

下载:

 [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>
 [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)
            };
        }

    }


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

以下是您应该如何操作。在控制器中创建如下操作:

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>


控制器:

 [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>
 [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 {


}

它没有下载..怎么办?请帮助我到底发生了什么?你得到FileStream对象了吗?如果我单击下载链接,显示空页。我使用的是google chrome。没有打开下载选项..你没有回答我的第二个问题。你得到FileStream对象了吗?我得到FileStream对象了。但是当我单击链接时,我得到了t在url中显示当前路径&未下载..这就是问题所在。