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
- <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"
- width="550" height="100" id="fileUpload" align="middle">
- <param name="allowScriptAccess" value="sameDomain" />
- <param name="movie" value="fileUpload.swf" />
- <param name="quality" value="high" />
- <param name="wmode" value="transparent">
- <param name="FlashVars" value='uploadPage=Upload.axd<%=GetFlashVars()%>&completeFunction=UploadComplete()'>
- <embed src="fileUpload.swf" flashvars='uploadPage=Upload.axd<%=GetFlashVars()%>&completeFunction=UploadComplete()'
- quality="high" wmode="transparent" width="550" height="100" name="fileUpload"
- align="middle" allowscriptaccess="sameDomain" type="application/x-shockwave-flash"
- pluginspage="http://www.macromedia.com/go/getflashplayer" />
- </object>
Expand|Select|Wrap|Line Numbers
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Request.Form != null)
- {
- //Upload obj = new Upload();
- //obj.ProcessRequest( );
- //Response.Write( Request.Form["__EVENTTARGET"].ToString());
- //Response.Write( Request.Form["__EVENTARGUMENT"].ToString());
- }
- Upload objImgfile = new Upload();
- //Session["titleid"] = 354;
- if (Upload.strmsg != "")
- {
- Response.Write(Upload.strmsg);
- Upload.strmsg = "";
- }
- // allows the javascript function to do a postback and call the onClick method
- // associated with the linkButton LinkButton1.
- string jscript = "function UploadComplete(){";
- jscript += string.Format("__doPostBack('{0}','');", LinkButton1.ClientID.Replace("_", "$"));
- jscript += "};";
- Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "FileCompleteUpload", jscript, true);
- }
- protected string GetFlashVars()
- {
- // Adds query string info to the upload page
- // you can also do something like:
- // return "?" + Server.UrlEncode("CategoryID="+CategoryID);
- // we UrlEncode it because of how LoadVars works with flash,
- // we want a string to show up like this 'CategoryID=3&UserID=4' in
- // the uploadPage variable in flash. If we passed this string withou
- // UrlEncode then flash would take UserID as a seperate LoadVar variable
- // instead of passing it into the uploadPage variable.
- // then in the httpHandler we get the CategoryID and UserID values from
- // the query string. See Upload.cs in App_Code
- return "?" + Server.UrlEncode(Request.QueryString.ToString());
- }
Expand|Select|Wrap|Line Numbers
- // According to http://msdn2.microsoft.com/en-us/library/system.web.httppostedfile.aspx
- // "Files are uploaded in MIME multipart/form-data format.
- // By default, all requests, including form fields and uploaded files,
- // larger than 256 KB are buffered to disk, rather than held in server memory."
- // So we can use an HttpHandler to handle uploaded files and not have to worry
- // about the server recycling the request do to low memory.
- // don't forget to increase the MaxRequestLength in the web.config.
- // If you server is still giving errors, then something else is wrong.
- // I've uploaded a 1.3 gig file without any problems. One thing to note,
- // when the SaveAs function is called, it takes time for the server to
- // save the file. The larger the file, the longer it takes.
- // So if a progress bar is used in the upload, it may read 100%, but the upload won't
- // be complete until the file is saved. So it may look like it is stalled, but it
- // is not.
- using System;
- using System.Data;
- using System.Configuration;
- 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;
- using System.IO;
- /// <summary>
- /// Upload handler for uploading files.
- /// </summary>
- public class Upload : IHttpHandler
- {
- public static string strmsg;
- public Upload()
- {
- }
- #region IHttpHandler Members
- public bool IsReusable
- {
- get { return true; }
- }
- public void ProcessRequest(HttpContext context)
- {
- // Example of using a passed in value in the query string to set a categoryId
- // Now you can do anything you need to witht the file.
- //int categoryId = 0;
- //if (context.Request.QueryString["CategoryID"] != null)
- //{
- // try
- // {
- // categoryId = Convert.ToInt32(context.Request.QueryString["CategoryID"]);
- // }
- // catch (Exception err)
- // {
- // categoryId = 0;
- // }
- //}
- //if (categoryId > 0)
- //{
- //}
- if (context.Request.Files.Count > 0)
- {
- // get the applications path
- string tempFile = context.Request.PhysicalApplicationPath;
- // loop through all the uploaded files
- for(int j = 0; j < context.Request.Files.Count; j++)
- {
- // get the current file
- HttpPostedFile uploadFile = context.Request.Files[j];
- // if there was a file uploded
- if (uploadFile.ContentLength > 0)
- {
- // save the file to the upload directory
- //use this if testing from a classic style upload, ie.
- // <form action="Upload.axd" method="post" enctype="multipart/form-data">
- // <input type="file" name="fileUpload" />
- // <input type="submit" value="Upload" />
- //</form>
- // this is because flash sends just the filename, where the above
- //will send the file path, ie. c:\My Pictures\test1.jpg
- //you can use Test.thm to test this page.
- //string filename = uploadFile.FileName.Substring(uploadFile.FileName.LastIndexOf("\\"));
- //uploadFile.SaveAs(string.Format("{0}{1}{2}", tempFile, "Upload\\", filename));
- // use this if using flash to upload
- uploadFile.SaveAs(string.Format("{0}{1}{2}", tempFile, "Upload\\", uploadFile.FileName));
- HttpContext.Current.Response.Write(tempFile);
- strmsg = tempFile;
- // HttpPostedFile has an InputStream also. You can pass this to
- // a function, or business logic. You can save it a database:
- //byte[] fileData = new byte[uploadFile.ContentLength];
- //uploadFile.InputStream.Write(fileData, 0, fileData.Length);
- // save byte array into database.
- // something I do is extract files from a zip file by passing
- // the inputStream to a function that uses SharpZipLib found here:
- // http://www.icsharpcode.net/OpenSource/SharpZipLib/
- // and then save the files to disk.
- }
- }
- }
- // Used as a fix for a bug in mac flash player that makes the
- // onComplete event not fire
- HttpContext.Current.Response.Write(" ");
- }
- #endregion
- }