Ejemplo n.º 1
0
 /// <summary>
 /// Handle and process events raised by the jQuery upload control
 /// </summary>
 /// <param name="objEvent"></param>
 protected override void FireEvent(IEvent objEvent)
 {
     if (objEvent.Type == "Fail")
     {
         #region Failed upload request
         string error = objEvent["error"].ToString();
         string res   = string.Format(SR.GetString("UploadControlError" + error), objEvent["result"].ToString());
         if (this.UploadErrorHandler != null)
         {
             this.UploadErrorHandler(this, new UploadErrorEventArgs(res, null));
         }
         else
         {
             throw new HttpException(res);
         }
         #endregion
     }
     else if (objEvent.Type == "Done")
     {
         #region Completed upload request
         long lngSize = 0;
         if (!long.TryParse(objEvent["Size"], out lngSize))
         {
             lngSize = 0;
         }
         UploadFileResult objResult = new UploadFileResult(
             objEvent["Name"].ToString(),
             lngSize,
             objEvent["Type"].ToString(),
             objEvent["TempName"]);
         if (this.UploadFileCompletedHandler != null)
         {
             this.UploadFileCompletedHandler(this, new UploadCompletedEventArgs(objResult));
         }
         #endregion
     }
     else if (objEvent.Type == "Start")
     {
         #region One or more files have been added to jQuery control, upload will now start
         if (this.UploadBatchStartingHandler != null)
         {
             this.UploadBatchStartingHandler(this, EventArgs.Empty);
         }
         #endregion
     }
     else if (objEvent.Type == "Stop")
     {
         #region Finished uploading batch of files
         if (this.UploadBatchCompletedHandler != null)
         {
             this.UploadBatchCompletedHandler(this, EventArgs.Empty);
         }
         #endregion
     }
     else
     {
         base.FireEvent(objEvent);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Process Ajax gateway upload request posted by the jQuery upload client component
        /// </summary>
        /// <param name="objHostContext"></param>
        /// <param name="strAction">The only supported action is "ULPost", posted by the jQuery upload control as Ajax gateway request</param>
        /// <returns></returns>
        protected override IGatewayHandler ProcessGatewayRequest(Gizmox.WebGUI.Hosting.HostContext objHostContext, string strAction)
        {
            if (strAction == WGAttributes.UploadControlPost)
            {
                // The response will be plain text.
                objHostContext.Response.ContentType = "text/plain";

                try
                {
                    // The byte range of current chunk being posted/uploaded
                    string strContentRange = (string)objHostContext.Request.Headers["Content-Range"];

                    // Current implementation as chunked uploads only support one file
                    if ((strContentRange != null) && (objHostContext.Request.Files.Count > 1))
                    {
                        throw new NotSupportedException("Chunked file uploads only supported for one file at a time");
                    }

                    // Prepare for list of uploaded file results - since chunked uploads are being used, there will be only one file in list
                    List <UploadFileResult> objUploadResults = new List <UploadFileResult>();

                    // Prepare serializer for json serialization of response to Ajax request
                    JavaScriptSerializer objJS = new JavaScriptSerializer();

                    // Get the posted file handle. Only one file, since we are using chunked uploads
                    HttpPostedFile objPostedFile       = objHostContext.HttpContext.Request.Files[0] as HttpPostedFile;
                    string         strUploadedFileName = string.Empty;

                    // Get filename without path
                    string[] files = objPostedFile.FileName.Split(new char[] { '\\' });
                    strUploadedFileName = files[files.Length - 1];

                    #region On first chunk of a new file, build temporary filename
                    if (strContentRange != null)
                    {
                        if (this.isFirstRange(strContentRange))
                        {
                            mstrChunkedFileName = System.IO.Path.Combine(this.UploadTempFilePath, Guid.NewGuid().ToString() + "_" + strUploadedFileName);
                        }
                        if (mstrChunkedFileName == null)
                        {
                            throw new NotSupportedException("Chunked file upload missing initial chunk");
                        }
                    }
                    #endregion

                    string savedFileName;
                    if (strContentRange == null)
                    {
                        #region If no range, file uploaded in one chunk, so save it
                        savedFileName = System.IO.Path.Combine(this.UploadTempFilePath, Guid.NewGuid().ToString() + "_" + strUploadedFileName);
                        objPostedFile.SaveAs(savedFileName);
                        #endregion
                    }
                    else
                    {
                        #region If range, save chunk. Assume sequential chunks
                        savedFileName = mstrChunkedFileName;
                        using (System.IO.FileStream fs = new System.IO.FileStream(savedFileName, System.IO.FileMode.Append, System.IO.FileAccess.Write))
                        {
                            byte[] buffer = new byte[UploadBufferSize];

                            long l = objPostedFile.InputStream.Read(buffer, 0, UploadBufferSize);
                            while (l > 0)
                            {
                                fs.Write(buffer, 0, (int)l);
                                l = objPostedFile.InputStream.Read(buffer, 0, UploadBufferSize);
                            }
                            fs.Flush();
                            fs.Close();
                        }
                        #endregion
                    }

                    // Prepare results for uploaded file request
                    UploadFileResult objResult = new UploadFileResult(strUploadedFileName, objPostedFile.ContentLength, objPostedFile.ContentType, savedFileName);
                    objUploadResults.Add(objResult);

                    #region Fire chunk completed event
                    if (this.UploadChunkRequestCompletedHandler != null)
                    {
                        UploadChunkRequestResult objChunkResult = new UploadChunkRequestResult(
                            strUploadedFileName,
                            (long)objPostedFile.ContentLength,
                            objPostedFile.ContentType,
                            savedFileName,
                            strContentRange);
                        this.UploadChunkRequestCompletedHandler(this, new UploadChunkRequestEventArgs(objChunkResult));
                    }
                    #endregion

                    // Prepare response as json serialized results object
                    object jsonObj = objJS.Serialize(objUploadResults.ToArray());
                    objHostContext.Response.Write(jsonObj.ToString());
                    return(null);
                }

                catch (Exception ex)
                {
                    #region Unhandled error detected. Throw exception, or call error handler
                    if (this.UploadErrorHandler != null)
                    {
                        this.UploadErrorHandler(this, new UploadErrorEventArgs(ex.Message, ex));
                    }
                    else
                    {
                        throw;
                    }
                    return(null);

                    #endregion
                }
            }

            // Call base, if action doesn't belong here.
            return(base.ProcessGatewayRequest(objHostContext, strAction));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes the arguments with the results for the uploaded file
 /// </summary>
 /// <param name="objList"></param>
 public UploadCompletedEventArgs(UploadFileResult objResult)
 {
     mobjResult = objResult;
 }