Ejemplo n.º 1
0
        public ActionResult Download(MemeViewModel meme)
        {
            try
            {
                MemeBL MemeBL = new MemeBL();
                MemeBL.DownloadMeme(meme);

            }
            catch (Exception ex)
            {
                //todo: log exception
            }
            return PartialView("~/Views/Meme/_MemeDisplay.cshtml", meme);
        }
Ejemplo n.º 2
0
        public void DownloadMeme(MemeViewModel meme)
        {
            //Create a stream for the file
            Stream stream = null;

            //This controls how many bytes to read at a time and send to the client
            int bytesToRead = 10000;

            // Buffer to read bytes in chunk size specified above
            byte[] buffer = new Byte[bytesToRead];

            // The number of bytes read
            try
            {
                //Create a WebRequest to get the file
                HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(meme.FileUrl);

                //Create a response for this request
                HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();

                if (fileReq.ContentLength > 0)
                    fileResp.ContentLength = fileReq.ContentLength;

                //Get the Stream returned from the response
                stream = fileResp.GetResponseStream();

                // prepare the response to the client. resp is the client Response
                var resp = HttpContext.Current.Response;

                //Indicate the type of data being sent
                resp.ContentType = "application/octet-stream";

                //Name the file
                resp.AddHeader("Content-Disposition", "attachment; filename=\"" + meme.FileName + "\"");
                resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

                int length;
                do
                {
                    // Verify that the client is connected.
                    if (resp.IsClientConnected)
                    {
                        // Read data into the buffer.
                        length = stream.Read(buffer, 0, bytesToRead);

                        // and write it out to the response's output stream
                        resp.OutputStream.Write(buffer, 0, length);

                        // Flush the data
                        resp.Flush();

                        //Clear the buffer
                        buffer = new Byte[bytesToRead];
                    }
                    else
                    {
                        // cancel the download if client has disconnected
                        length = -1;
                    }
                } while (length > 0); //Repeat until no data is read
            }
            finally
            {
                if (stream != null)
                {
                    //Close the input stream
                    stream.Close();
                }
            }
        }
Ejemplo n.º 3
0
        public MemeViewModel GetMeme(string filename)
        {
            HttpContextWrapper context = new HttpContextWrapper(System.Web.HttpContext.Current);
            var request = context.Request;
            var url = request.Url.ToString();
            var uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri).GetLeftPart(UriPartial.Authority);

            if (!url.Contains(filename))
            {
                url = url.Replace("All", "Show") + "/" + filename;
            }

            MemeViewModel meme = new MemeViewModel
            {
                MemeFileType = ViewModels.MemeFileType.Created,
                FileName = filename + Constants.IMAGE_EXTENSION,
                ShareUrl = url,
                FileUrl = uri + MemeBL.GetStoreVirtualPath() + filename + Constants.IMAGE_EXTENSION
            };
            return meme;
        }