Beispiel #1
0
        public void ProcessRequest(HttpContext context)
        {
            string id = context.Request.QueryString["id"];
            string filename = context.Request.QueryString["filename"];
            bool complete = string.IsNullOrEmpty(context.Request.QueryString["Complete"]) ? true : bool.Parse(context.Request.QueryString["Complete"]);
            bool getBytes = string.IsNullOrEmpty(context.Request.QueryString["GetBytes"]) ? false : bool.Parse(context.Request.QueryString["GetBytes"]);
            long startByte = string.IsNullOrEmpty(context.Request.QueryString["StartByte"]) ? 0 : long.Parse(context.Request.QueryString["StartByte"]);

            var blobUri = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}/{1}{2}",
                    ConfigReader.GetConfigValue("MainBlobContanier"),
                    id,
                    Path.GetExtension(filename));

            var du = new DistributedUpload(
                CloudStorageAccount.FromConfigurationSetting("DataConnectionString"),
                blobUri);

            if (getBytes)
            {
                try
                {
                    du.Begin();
                }
                catch (Exception ex)
                {
                    context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                    context.Response.StatusDescription = ex.Message;
                    return;
                }

                context.Response.Write("0");
                return;
            }
            else
            {
                try
                {
                    this.UploadChunkToBlobStorage(id, startByte, context.Request.InputStream, du);
                }
                catch (Exception ex)
                {
                    context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                    context.Response.StatusDescription = ex.Message;
                    return;
                }

                if (complete)
                {
                    if (this.FileUploadCompleted != null)
                    {
                        var args = new FileUploadCompletedEventArgs(filename, du);
                        this.FileUploadCompleted(this, args);
                    }
                }
            }
        }
        private void FileUploadFileUploadCompleted(object sender, FileUploadCompletedEventArgs e)
        {
            var dataStore = BlobShareDataStoreEntities.CreateInstance();

            try
            {
                var blobService = new BlobService(
                    dataStore,
                    CloudStorageAccount.Parse(ConfigReader.GetConfigValue("DataConnectionString")),
                    ConfigReader.GetConfigValue("MainBlobContanier"));

                try
                {
                    if (!string.IsNullOrEmpty(e.FileName))
                    {
                        e.DistributedUpload.Commit();

                        var resource = new Blob();
                        resource.BlobId = Guid.Parse(this.ctx.Request.QueryString["id"]);
                        resource.Name = Path.GetFileNameWithoutExtension(e.FileName);
                        resource.Description = string.Empty;
                        resource.OriginalFileName = e.FileName;
                        resource.UploadDateTime = DateTime.UtcNow;

                        blobService.CreateBlob(resource);
                    }
                }
                catch (Exception ex)
                {
                    this.ctx.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
                    this.ctx.Response.StatusDescription = ex.Message;
                    return;
                }
            }
            finally
            {
                dataStore.Dispose();
            }
        }