0

I've been searching for long time of solving the limitation in asp file uploading as the case in my question File Upload: Fail to assign value into File, it still not answered by anybody. At this moment, can i know is there anybody here know how to use session to store an image for temporary, and thereafter retrieve it back to the stream and put it into the model?

Community
  • 1
  • 1
dayrlism
  • 17
  • 1
  • 9

2 Answers2

0

You could convert the image into a byte array then convert it to a base64 string and save that into the session. Then convert it to a byte array and bind it to your control.

  • example now i have the file is in the model.file the type is httppostedfilebase, how i should convert it to byte array? or can it directly store in session? – dayrlism Oct 01 '14 at 02:01
  • http://stackoverflow.com/questions/7852102/convert-httppostedfilebase-to-byte once you have the byte array you; string str = convert.toBase64String(bytearr); – udonNoodles Oct 01 '14 at 02:17
  • the one we talking is storing or retrieving? sorry to ask – dayrlism Oct 01 '14 at 02:27
  • i think we are talking about storing right, i done for that wht about retrieving? by the way izzit correct? – dayrlism Oct 01 '14 at 02:31
  • MemoryStream target = new MemoryStream(); model.File.InputStream.CopyTo(target); byte[] data = target.ToArray(); string place = Convert.ToBase64String(data); Session["File1"] = place; – dayrlism Oct 01 '14 at 02:31
  • i try using the session variable to assign it to model.file, but it keep fail me saying that cannot convert object to httpfilebase. do you have any idea. BTW, you know why i want to assign it back to model.file right? – dayrlism Oct 01 '14 at 02:34
  • Well you have to convert it back to the format you want it. So now its just a string so now convert the string back into a byte array and stream it back into the model.file format. – udonNoodles Oct 01 '14 at 03:19
0

I finally found the solution for it. ;)

        if (model.File != null && model.File.ContentLength > 0)
        {
            Byte[] destination1 = new Byte[model.File.ContentLength];
            model.File.InputStream.Position = 0;
            model.File.InputStream.Read(destination1, 0, model.File.ContentLength);
            model.BankSlip = destination1;
            Session["info.file"]= model.File;//storing session.
        }
        else
        {
            //retrieving session
            var myImg1 = Session["info.file"] as HttpPostedFileBase;
            model.File = myImg1;
            Byte[] data=new Byte[myImg1.ContentLength];
            myImg1.InputStream.Position = 0;
            myImg1.InputStream.Read(data, 0, myImg1.ContentLength);
            model.BankSlip = data;
        }
        }
        catch (Exception ex)
        {                   
            DepositControllerLog.ErrorException("DepositController - LocalBank(Post) - AddAttachment(refreshed) - ", ex);
        }
        }
dayrlism
  • 17
  • 1
  • 9