コード例 #1
0
        /// <summary>
        /// Process the file.
        /// </summary>
        private void ProcessFile(object State)
        {
            UploaderControl2ProcessFileState processFileState = State as UploaderControl2ProcessFileState;

            try
            {
                // the path to where this file will be written on disk
                string sandboxPath = Path.Combine(Sandbox.UploaderControlSandboxPath, processFileState.Guid);

                // Is it the first chunk or is the upload for this file being canceled? Create the file on the server
                if (processFileState.Cancel || processFileState.FirstChunk)
                {
                    // if the file already exists in the sandbox, delete it
                    if (File.Exists(sandboxPath))
                    {
                        File.Delete(sandboxPath);
                    }
                }

                if (!processFileState.Cancel)
                {
                    // if the process flag is set to true, the entire file has been sent, so call the custom file processor
                    if (processFileState.Process && !string.IsNullOrEmpty(processFileState.UploadedFileProcessorType))
                    {
                        string typeName  = processFileState.UploadedFileProcessorType;
                        string className = typeName.Substring(0, typeName.IndexOf(",")).Trim();
                        IUploadedFileProcessor processor = Type.GetType(typeName, true).Assembly.CreateInstance(className) as IUploadedFileProcessor;
                        processor.ProcessFile(processFileState.AsyncResult.Context, processFileState.Guid, processFileState.Name, processFileState.ContextParam);
                    }
                    else
                    {
                        // this is another chunk of data
                        byte[] buffer = new byte[4096];
                        int    bytesRead;
                        using (FileStream fs = File.Open(sandboxPath, FileMode.Append))
                        {
                            while ((bytesRead = processFileState.AsyncResult.Context.Request.InputStream.Read(buffer, 0, buffer.Length)) != 0)
                            {
                                fs.Write(buffer, 0, bytesRead);
                            }
                        }
                    }
                }

                processFileState.AsyncResult.CompleteCall();
            }
            catch (Exception ex)
            {
                processFileState.AsyncResult.Exception = ex;
                processFileState.AsyncResult.CompleteCall();
            }
        }
コード例 #2
0
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            UploaderControl2AsyncResult result = new UploaderControl2AsyncResult(context, cb, extraData);

            // parse the parameters sent to the request and bundle them up to be passed to the worker thread
            UploaderControl2ProcessFileState processFileState = new UploaderControl2ProcessFileState(context, result);

            // do all of the work on a custom thread pool so that the asp.net thread pool doesn't get blocked up

            // TODO: This example uses the built-in ThreadPool, but you should use a custom thread pool!  The built-in thread pool
            // draws from the same threads that the web server uses to service web requests, so you will see no benefit.
            // See the codeplex project for some links to open source custom thread pool implementations.

            // change this line to queue ProcessFile onto your custom thread pool
            ThreadPool.QueueUserWorkItem(ProcessFile, processFileState);

            return result;
        }
コード例 #3
0
        public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
        {
            UploaderControl2AsyncResult result = new UploaderControl2AsyncResult(context, cb, extraData);

            // parse the parameters sent to the request and bundle them up to be passed to the worker thread
            UploaderControl2ProcessFileState processFileState = new UploaderControl2ProcessFileState(context, result);

            // do all of the work on a custom thread pool so that the asp.net thread pool doesn't get blocked up

            // TODO: This example uses the built-in ThreadPool, but you should use a custom thread pool!  The built-in thread pool
            // draws from the same threads that the web server uses to service web requests, so you will see no benefit.
            // See the codeplex project for some links to open source custom thread pool implementations.

            // change this line to queue ProcessFile onto your custom thread pool
            ThreadPool.QueueUserWorkItem(ProcessFile, processFileState);

            return(result);
        }