0

I have two actions and I want to send HttpPostedFile from first action to another action using session or tempdata. who can help realize it this is my code, exeption in xdocument load - IIs Express/*.xml don't found

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



    }
Nilesh
  • 2,583
  • 5
  • 21
  • 34
  • 1
    You should not try putting a file into a session variable. Why don't you upload the file to a reserved folder on the server, maybe give it a UID to mark it uniquely, and then simply pass a reference to that file to the other action? – Rob Mar 28 '14 at 07:41
  • Dear Robert thanks for your answer but in my app I can't do like you say because this longer code and every time it upload save in server and afther using need delete – user3359460 Mar 28 '14 at 07:49
  • check if your session variable is not exceeding allowed memory. take a look at this question: http://stackoverflow.com/questions/2843237/asp-net-session-size-limitation – SHM Mar 28 '14 at 09:31
  • no my files is only 80-500 byte – user3359460 Mar 28 '14 at 11:21
  • Why don't you just pass on the content of the file instead of the entire file? This way you will not have to save it and can easily use `TempData` which will be cleaned up once you access the content. And you dont have to worry about the session cleanup and stuff. – Nilesh Mar 31 '14 at 09:27

1 Answers1

0

Try this. Hope it will work. I din't gave a try.

[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();
    }
}
Vishal Vaishya
  • 596
  • 3
  • 15
  • another exception Line 103: XDocument xml = XDocument.Load(content); Illegal characters in path. – user3359460 Mar 28 '14 at 12:52
  • Check your file content. Generally this error is caused due to not well formed xml document. occurs when any closing tag is missing or node name contains special characters. You can validate your content here [freeformatter.com](http://www.freeformatter.com/xml-formatter.html) – Vishal Vaishya Mar 29 '14 at 14:12
  • but this alternativ way) – user3359460 Apr 01 '14 at 06:36