如何使用mvc中的session或tempdata将文件从操作发送到其他操作

时间:2014-03-28 07:26:24

标签: c# asp.net-mvc model-view-controller

我有两个动作,我想使用session或tempdata将HttpPostedFile从第一个动作发送到另一个动作。谁可以帮助实现它这是我的代码,xdocument中的exeption load - IIs Express / * .xml找不到

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase fil)
    {

        if (fil != null && fil.ContentLength > 0)
        {


            Session["doc2"] = fil;

        }

        return RedirectToAction("Xmlview");
    }

    [HttpGet]
    public ActionResult Xmlview()
    {

        HttpPostedFileBase file2= Session["doc2"] as HttpPostedFileBase;

        var fileex = Path.GetExtension(file2.FileName);

        var fileName = Path.GetFullPath(file2.FileName);

        string xmlstr = ".xml";

        Value model2 = new Value();
        string strall = "";

        if (fileex.Contains(xmlstr))
        {
        XDocument xml = XDocument.Load(fileName);  // exeption hear IIs Express/*.xml don't found
        var allElements = xml.Elements();



    }

1 个答案:

答案 0 :(得分:0)

试试这个。希望它会奏效。我没试过。

[HttpPost]
public ActionResult Index(HttpPostedFileBase fil)
{
    if (fil != null && fil.ContentLength > 0)
    {
        // Read bytes from http input stream
        BinaryReader b = new BinaryReader(file.InputStream);
        byte[] binData = b.ReadBytes(file.InputStream.Length);
        string result = System.Text.Encoding.UTF8.GetString(binData);
        Session["doc2"] = result;

    }
    return RedirectToAction("Xmlview");
}

[HttpGet]
public ActionResult Xmlview()
{
    Value model2 = new Value();
    string strall = "";
    string content = Session["doc2"].ToString();
    if (!String.IsNullOrEmpty(content))
    {
        XDocument xml = XDocument.Parse(content);  
        var allElements = xml.Elements();
    }
}