Saturday, March 24, 2012

Upload Progress Bar

So I'm taking a file and loading it into a stream and then displaying that file. I'm wondering if there is a way to add a progress bar that will show how much of the upload has finished. I've googled for something that tells me how to track the completion of the upload into the stream, but couldn't find anything.

Can someone point me in the right direction?

Here's my code:


1FileStream fs =new FileStream(FileUpload1.FileName, FileMode.OpenOrCreate, FileAccess.Read);
2 Byte[] img =new Byte[fs.Length];
34 fs.Read(img, 0, Convert.ToInt32(fs.Length));
56 System.IO.MemoryStream ms =new System.IO.MemoryStream(img);
7//System.IO.Stream ps = new System.IO.MemoryStream(img);
8 //System.Drawing.Bitmap b = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(ms);
9 //System.Drawing.Bitmap b = new System.Drawing.Bitmap(ps);1011 System.Drawing.Bitmap sb = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
1213 sb.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
On the other hand, what I need is an image upload with a preview and upload progress bar, where the preview is cached/dynamically generated and not stored on the disk. 
Thanks!

Hi,rksprst

Need a preview, you can do it like this:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default25 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string oIP = Context.Request.QueryString["oIP"];
MakeThumbnail(oIP, oIP + ".JPG", 100, 0, "W");
}
/// <summary>
///
/// </summary>
/// <param name="originalImagePath"></param>
/// <param name="thumbnailPath"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="mode"></param>
public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

int towidth = width;
int toheight = height;

int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;

switch (mode)
{
case "HW":
break;
case "W":
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H":
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut":
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}


System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);


System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);


g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;


g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;


g.Clear(System.Drawing.Color.Transparent);


g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
try
{
HttpResponse httpResponse = Context.Response;
httpResponse.Clear();
httpResponse.ContentType = "Image/Jpeg";
bitmap.Save(httpResponse.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
httpResponse.End();
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}

Use it like this:

<%@. Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
ImageMap1.ImageUrl = "Default25.aspx?oIP=C:\\Documents and Settings\\v-jyyin\\Desktop\\IMG\\c4960543c39f4f0c71b53bf27aa34e65.jpg";
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server" method="get">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:ImageMap ID="ImageMap1" runat="server">
</asp:ImageMap></div>
</form>
</body>
</html>

By the way,I don't think you can get the progress bar without ActiveX.

No comments:

Post a Comment