Beispiel #1
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));
        }
Beispiel #2
0
 /// <summary>
 /// Initializes the arguments with UploadFileResults
 /// </summary>
 /// <param name="objResult"></param>
 public UploadChunkRequestEventArgs(UploadChunkRequestResult objResult)
 {
     mobjResult = objResult;
 }