By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,008 Members | 2,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,008 IT Pros & Developers. It's quick & easy.

Progress bar implementation with flash + .Net. workin on local system.

P: 3
Hi,

I have implement multiple file uploading progress bar with the help of flash and .net file is upload on my local machine but not working with server it's give error while uploading image on server. code is as follows.

this is flash object:
Expand|Select|Wrap|Line Numbers
  1.  <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"
  2.                 width="550" height="100" id="fileUpload" align="middle">
  3.                 <param name="allowScriptAccess" value="sameDomain" />
  4.                 <param name="movie" value="fileUpload.swf" />
  5.                 <param name="quality" value="high" />
  6.                 <param name="wmode" value="transparent">
  7.                 <param name="FlashVars" value='uploadPage=Upload.axd<%=GetFlashVars()%>&completeFunction=UploadComplete()'>
  8.                 <embed src="fileUpload.swf" flashvars='uploadPage=Upload.axd<%=GetFlashVars()%>&completeFunction=UploadComplete()'
  9.                     quality="high" wmode="transparent" width="550" height="100" name="fileUpload"
  10.                     align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash"
  11.                     pluginspage="http://www.macromedia.com/go/getflashplayer" />
  12.             </object>
  13.  
Code behind :
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.         if (Request.Form != null)
  4.         {
  5.             //Upload obj = new Upload();
  6.             //obj.ProcessRequest( );
  7.             //Response.Write( Request.Form["__EVENTTARGET"].ToString());
  8.             //Response.Write( Request.Form["__EVENTARGUMENT"].ToString());
  9.         }
  10.  
  11.         Upload objImgfile = new Upload();
  12.         //Session["titleid"] = 354;
  13.         if (Upload.strmsg != "")
  14.         {
  15.             Response.Write(Upload.strmsg);
  16.             Upload.strmsg = "";
  17.         }
  18.  
  19.         // allows the javascript function to do a postback and call the onClick method
  20.         // associated with the linkButton LinkButton1.
  21.         string jscript = "function UploadComplete(){";
  22.         jscript += string.Format("__doPostBack('{0}','');", LinkButton1.ClientID.Replace("_", "$"));
  23.         jscript += "};";
  24.         Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "FileCompleteUpload", jscript, true);
  25.     }
  26.  
  27.     protected string GetFlashVars()
  28.     {
  29.         // Adds query string info to the upload page
  30.         // you can also do something like:
  31.         // return "?" + Server.UrlEncode("CategoryID="+CategoryID);
  32.         // we UrlEncode it because of how LoadVars works with flash,
  33.         // we want a string to show up like this 'CategoryID=3&UserID=4' in
  34.         // the uploadPage variable in flash.  If we passed this string withou
  35.         // UrlEncode then flash would take UserID as a seperate LoadVar variable
  36.         // instead of passing it into the uploadPage variable.
  37.         // then in the httpHandler we get the CategoryID and UserID values from 
  38.         // the query string. See Upload.cs in App_Code
  39.         return "?" + Server.UrlEncode(Request.QueryString.ToString());
  40.     }
  41.  
and this code use one cs file to upload image which is as follows:
Expand|Select|Wrap|Line Numbers
  1. // According to http://msdn2.microsoft.com/en-us/library/system.web.httppostedfile.aspx
  2. // "Files are uploaded in MIME multipart/form-data format. 
  3. // By default, all requests, including form fields and uploaded files, 
  4. // larger than 256 KB are buffered to disk, rather than held in server memory."
  5. // So we can use an HttpHandler to handle uploaded files and not have to worry
  6. // about the server recycling the request do to low memory. 
  7. // don't forget to increase the MaxRequestLength in the web.config.
  8. // If you server is still giving errors, then something else is wrong.
  9. // I've uploaded a 1.3 gig file without any problems. One thing to note, 
  10. // when the SaveAs function is called, it takes time for the server to 
  11. // save the file. The larger the file, the longer it takes.
  12. // So if a progress bar is used in the upload, it may read 100%, but the upload won't
  13. // be complete until the file is saved.  So it may look like it is stalled, but it
  14. // is not.
  15.  
  16. using System;
  17. using System.Data;
  18. using System.Configuration;
  19. using System.Web;
  20. using System.Web.Security;
  21. using System.Web.UI;
  22. using System.Web.UI.WebControls;
  23. using System.Web.UI.WebControls.WebParts;
  24. using System.Web.UI.HtmlControls;
  25. using System.IO;
  26.  
  27. /// <summary>
  28. /// Upload handler for uploading files.
  29. /// </summary>
  30. public class Upload : IHttpHandler
  31. {
  32.     public static string strmsg;
  33.     public Upload()
  34.     {
  35.     }
  36.  
  37.     #region IHttpHandler Members
  38.  
  39.     public bool IsReusable
  40.     {
  41.         get { return true; }
  42.     }
  43.  
  44.     public void ProcessRequest(HttpContext context)
  45.     {
  46.         // Example of using a passed in value in the query string to set a categoryId
  47.         // Now you can do anything you need to witht the file.
  48.         //int categoryId = 0;
  49.         //if (context.Request.QueryString["CategoryID"] != null)
  50.         //{
  51.         //    try
  52.         //    {
  53.         //        categoryId = Convert.ToInt32(context.Request.QueryString["CategoryID"]);
  54.         //    }
  55.         //    catch (Exception err)
  56.         //    {
  57.         //        categoryId = 0;
  58.         //    }
  59.         //}
  60.         //if (categoryId > 0)
  61.         //{
  62.         //}
  63.  
  64.         if (context.Request.Files.Count > 0)
  65.         {
  66.             // get the applications path
  67.             string tempFile = context.Request.PhysicalApplicationPath;
  68.             // loop through all the uploaded files
  69.             for(int j = 0; j < context.Request.Files.Count; j++)
  70.             {
  71.                 // get the current file
  72.                 HttpPostedFile uploadFile = context.Request.Files[j];
  73.                 // if there was a file uploded
  74.                 if (uploadFile.ContentLength > 0)
  75.                 {
  76.                     // save the file to the upload directory
  77.  
  78.                     //use this if testing from a classic style upload, ie. 
  79.  
  80.                     // <form action="Upload.axd" method="post" enctype="multipart/form-data">
  81.                     //    <input type="file" name="fileUpload" />
  82.                     //    <input type="submit" value="Upload" />
  83.                     //</form>
  84.  
  85.                     // this is because flash sends just the filename, where the above 
  86.                     //will send the file path, ie. c:\My Pictures\test1.jpg
  87.                     //you can use Test.thm to test this page.
  88.                     //string filename = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf("\\"));
  89.                     //uploadFile.SaveAs(string.Format("{0}{1}{2}", tempFile, "Upload\\", filename));
  90.  
  91.                     // use this if using flash to upload
  92.                     uploadFile.SaveAs(string.Format("{0}{1}{2}", tempFile, "Upload\\", uploadFile.FileName));
  93.                     HttpContext.Current.Response.Write(tempFile);
  94.                     strmsg = tempFile; 
  95.                     // HttpPostedFile has an InputStream also.  You can pass this to 
  96.                     // a function, or business logic. You can save it a database:
  97.  
  98.                     //byte[] fileData = new byte[uploadFile.ContentLength];
  99.                     //uploadFile.InputStream.Write(fileData, 0, fileData.Length);
  100.                     // save byte array into database.
  101.  
  102.                     // something I do is extract files from a zip file by passing
  103.                     // the inputStream to a function that uses SharpZipLib found here:
  104.                     // http://www.icsharpcode.net/OpenSource/SharpZipLib/
  105.                     // and then save the files to disk.                    
  106.                 }                
  107.             }
  108.         }
  109.         // Used as a fix for a bug in mac flash player that makes the 
  110.         // onComplete event not fire
  111.         HttpContext.Current.Response.Write(" ");
  112.     }
  113.  
  114.     #endregion
  115. }
Nov 14 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.