Search code examples
c#asp.net-mvcimagesession-variablestemporary-files

Session Storing Images for Temporary and retrive and store into model


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?


Solution

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