Esempio n. 1
0
        void IHttpHandler.ProcessRequest(HttpContext context)
        {
            BlobHandlerInfo handler = BlobHandlerInfo.Current;

            if (handler == null)
            {
                throw new HttpException(404, String.Empty);
            }
            if (handler.Mode == BlobMode.Upload)
            {
                RenderUploader(context, handler, handler.SaveFile(context));
            }
            else
            {
                string tempFileName = Path.GetTempFileName();
                Stream stream       = File.Create(tempFileName);
                handler.LoadFile(stream);
                CopyToOutput(context, stream, handler);
                stream.Close();
                File.Delete(tempFileName);
            }
            if (handler.Mode != BlobMode.Thumbnail)
            {
                context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            }
        }
Esempio n. 2
0
        private void RenderUploader(HttpContext context, BlobHandlerInfo handler, bool uploadSuccess)
        {
            HtmlTextWriter writer = new HtmlTextWriter(context.Response.Output);

            writer.WriteLine("<!DOCTYPE html PUBLIC \\\"-//W3C//DTD XHTML 1.0 Transitional//EN\\\" \\\"http://www.w3." +
                             "org/TR/xhtml1/DTD/xhtml1-transitional.dtd\\\">");
            writer.AddAttribute("xmlns", "http://www.w3.org/1999/xhtml");
            writer.RenderBeginTag(HtmlTextWriterTag.Html);
            // head
            writer.RenderBeginTag(HtmlTextWriterTag.Head);
            writer.RenderBeginTag(HtmlTextWriterTag.Title);
            writer.Write("Uploader");
            writer.RenderEndTag();
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
            writer.RenderBeginTag(HtmlTextWriterTag.Script);
            writer.WriteLine(@" 
function ShowUploadControls() { 
    document.getElementById('UploadControlsPanel').style.display ='block'; 
    document.getElementById('StartUploadPanel').style.display = 'none';   
    document.getElementById('FileUpload').focus();      
} 
function Owner() {
    var m = window.location.href.match(/owner=(.+?)&/);
    return m ? parent.$find(m[1]) : null;
}
function StartUpload(msg) {
    if (msg && !window.confirm(msg)) return;
    if (parent && parent.window.Web) {
        var m = window.location.href.match(/&index=(\d+)$/);
        if (m) Owner()._showUploadProgress(m[1]);
    }
    document.forms[0].submit();
}
function UploadSuccess(key, message) { 
    if (parent && parent.window.Web) { 
        parent.Web.DataView.showMessage(message); 
        Owner().refresh();
    }     
    else 
        alert('Success');         
}");
            writer.RenderEndTag();
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
            writer.RenderBeginTag(HtmlTextWriterTag.Style);
            writer.WriteLine("body{font-family:tahoma;font-size:8.5pt;margin:4px;background-color:white;}");
            writer.WriteLine("input{font-family:tahoma;font-size:8.5pt;}");
            writer.WriteLine("input.FileUpload{padding:3px}");
            writer.RenderEndTag();
            writer.RenderEndTag();
            // body
            string message = null;

            if (uploadSuccess)
            {
                if (HttpContext.Current.Request.Files[0].ContentLength > 0)
                {
                    message = String.Format("<b>Confirmation:</b> {0} has been uploaded successfully. <b>It may take up to {1}" +
                                            " minutes for the thumbnail to reflect the uploaded content.</b>", handler.Text.ToLower(), Blob.ThumbnailCacheTimeout);
                }
                else
                {
                    message = String.Format("<b>Confirmation:</b> {0} has been cleared.", handler.Text.ToLower());
                }
            }
            else
            if (!(String.IsNullOrEmpty(handler.Error)))
            {
                message = String.Format("<b>Error:</b> failed to upload {0}. {1}", handler.Text.ToLower(), handler.Error.Replace("\'", "\\\'"));
            }
            if (!(String.IsNullOrEmpty(message)))
            {
                writer.AddAttribute("onload", String.Format("UploadSuccess(\'{0}={1}\', \'{2}\')", handler.Key, handler.Value.Replace("u|", "t|"), message));
            }
            writer.RenderBeginTag(HtmlTextWriterTag.Body);
            // form
            writer.AddAttribute(HtmlTextWriterAttribute.Name, "form1");
            writer.AddAttribute("method", "post");
            writer.AddAttribute("action", context.Request.RawUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "form1");
            writer.AddAttribute("enctype", "multipart/form-data");
            writer.RenderBeginTag(HtmlTextWriterTag.Form);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            // begin "start upload" controls
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "StartUploadPanel");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.Write("Click ");
            writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "ShowUploadControls();return false");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write("here");
            writer.RenderEndTag();
            writer.Write(" to upload or clear {0} file.", handler.Text.ToLower());
            // end of "start upload" controls
            writer.RenderEndTag();
            // begin "upload controls"
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "UploadControlsPanel");
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "display:none");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            // "FileUpload" input
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "File");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, "FileUpload");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "FileUpload");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "FileUpload");
            writer.AddAttribute(HtmlTextWriterAttribute.Onchange, "StartUpload()");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
            // "FileClear" input
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "FileClear");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "FileClear");
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "StartUpload(\'Clear?\')");
            writer.AddAttribute(HtmlTextWriterAttribute.Value, "Clear");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
            // end of "upload controls"
            writer.RenderEndTag();
            // close "div"
            writer.RenderEndTag();
            // close "form"
            writer.RenderEndTag();
            // close "body"
            writer.RenderEndTag();
            // close "html"
            writer.RenderEndTag();
            writer.Close();
        }
Esempio n. 3
0
        private void CopyToOutput(HttpContext context, Stream stream, BlobHandlerInfo handler)
        {
            int offset = 0;

            stream.Position = offset;
            byte[] buffer = null;
            Image  img    = null;

            // attempt to auto-detect content type as an image
            if (String.IsNullOrEmpty(handler.ContentType) && (stream.Length > 0))
            {
                try
                {
                    img = Image.FromStream(stream);
                }
                catch (Exception)
                {
                    try
                    {
                        // Correction for Northwind database image format
                        offset          = 78;
                        stream.Position = offset;
                        buffer          = new byte[(stream.Length - offset)];
                        stream.Read(buffer, 0, buffer.Length);
                        img = Image.FromStream(new MemoryStream(buffer, 0, buffer.Length));
                    }
                    catch (Exception ex)
                    {
                        context.Trace.Write(ex.ToString());
                    }
                }
            }
            // send an original or a thumbnail to the output
            if (handler.Mode == BlobMode.Thumbnail)
            {
                // draw a thumbnail
                Bitmap    thumbnail = new Bitmap(92, 71);
                Graphics  g         = Graphics.FromImage(thumbnail);
                Rectangle r         = new Rectangle(0, 0, 91, 70);
                g.FillRectangle(Brushes.White, r);
                g.DrawRectangle(Pens.Silver, r);
                double thumbnailAspect = (Convert.ToDouble(r.Height) / Convert.ToDouble(r.Width));
                if (img != null)
                {
                    r.Inflate(-2, -2);
                    if (img.Width > img.Height)
                    {
                        r.Height = Convert.ToInt32((Convert.ToDouble(r.Width) * thumbnailAspect));
                        r.Width  = Convert.ToInt32((Convert.ToDouble(r.Height)
                                                    * (Convert.ToDouble(img.Width) / Convert.ToDouble(img.Height))));
                    }
                    else
                    if (img.Height > img.Width)
                    {
                        r.Width  = Convert.ToInt32((Convert.ToDouble(r.Height) * thumbnailAspect));
                        r.Height = Convert.ToInt32((Convert.ToDouble(r.Width)
                                                    * (Convert.ToDouble(img.Height) / Convert.ToDouble(img.Width))));
                    }
                    else
                    {
                        r.Width  = Convert.ToInt32((Convert.ToDouble(img.Height) * thumbnailAspect));
                        r.Height = r.Width;
                    }
                    double aspect = (Convert.ToDouble((thumbnail.Width - 4)) / r.Width);
                    if (r.Width <= r.Height)
                    {
                        aspect = (Convert.ToDouble((thumbnail.Height - 4)) / r.Height);
                    }
                    if (aspect > 1)
                    {
                        aspect = 1;
                    }
                    r.Width    = Convert.ToInt32((Convert.ToDouble(r.Width) * aspect));
                    r.Height   = Convert.ToInt32((Convert.ToDouble(r.Height) * aspect));
                    r.Location = new Point(((thumbnail.Width - r.Width)
                                            / 2), ((thumbnail.Height - r.Height)
                                                   / 2));
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.DrawImage(img, r);
                }
                else
                {
                    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                    Font f = new Font("Arial", ((float)(7.5)));
                    g.DrawString(handler.Text, f, Brushes.Black, r);
                }
                // produce thumbnail data
                MemoryStream ts = new MemoryStream();
                thumbnail.Save(ts, ImageFormat.Png);
                ts.Flush();
                ts.Position = 0;
                byte[] td = new byte[ts.Length];
                ts.Read(td, 0, td.Length);
                ts.Close();
                // Send thumbnail to the output
                context.Response.AddHeader("Content-Length", td.Length.ToString());
                context.Response.ContentType = "image/png";
                context.Response.OutputStream.Write(td, 0, td.Length);
                if (img == null)
                {
                    context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                }
                else
                {
                    context.Response.Cache.SetCacheability(HttpCacheability.Public);
                    context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(Blob.ThumbnailCacheTimeout));
                }
            }
            else
            {
                string contentType = handler.ContentType;
                if (img != null)
                {
                    contentType = ImageFormats[img.RawFormat.Guid];
                }
                if (String.IsNullOrEmpty(contentType))
                {
                    contentType = "application/octet-stream";
                }
                string fileName = handler.FileName;
                if (String.IsNullOrEmpty(fileName))
                {
                    fileName = String.Format("{0}{1}.{2}", handler.Key, handler.Reference, contentType.Substring((contentType.IndexOf("/") + 1)));
                }
                context.Response.ContentType = contentType;
                //context.Response.AddHeader("Content-Disposition", ("filename=" + fileName));
                //context.Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
                context.Response.AppendHeader("content-disposition", "filename=" + fileName);
                //context.Response.AddHeader("Content-Length", stream.Length.ToString());
                //context.Response.AddHeader("content-length", stream.Length.ToString());
                stream.Position = offset;
                buffer          = new byte[(1024 * 32)];
                int bytesRead = stream.Read(buffer, 0, buffer.Length);
                while (bytesRead > 0)
                {
                    context.Response.OutputStream.Write(buffer, 0, bytesRead);
                    offset    = (offset + bytesRead);
                    bytesRead = stream.Read(buffer, 0, buffer.Length);
                }
            }
        }