Session Storing Images for Temporary and retrive and store into model

为君一笑 提交于 2020-01-17 08:41:29

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/26132792/session-storing-images-for-temporary-and-retrive-and-store-into-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!