Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

w3p3 IIS process blocking file deletion

$
0
0

Hi,

I am successfully uploading pictures from a webpage to my server, creating some OpenXML and downloading that file. Now, after that, I would like to delete the pictures that were uploaded. But everytime I get a "xx.jpg is used by another process - can't be deleted". I managed to find out it is the w3p3 process still using it, although all my actions are completed. Even after closing the browser and stopping any client requests etc. it is still being used. I than have to restart the server or kill the process to get rid of the pictures.

Here is my Code for Uploading and using the pics in OpenXML… I don't see the trouble... The Upload function gets called from Dropzone once for each picture.

Thank you for your help!

    [HttpPost]

        public async Task<ActionResult> UploadFiles()
        {
            // Methode wird für jedes pic aufgerufen

            var files = HttpContext.Request.Form.Files;

            System.Security.Claims.ClaimsPrincipal currentUser = this.User;
            string uname = currentUser.Identity.Name;

            string folderpath = @"D:\Uploaded_Files\" + uname;

            Random rand = new Random();
            int r = rand.Next(0, 10000);

            while (Directory.Exists(folderpath + @"\" + r + ".jpg"))
                r = rand.Next(0, 10000);

            string filePath = folderpath + @"\" + r + ".jpg";

            if (files[0].Length > 0)
            {     using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await files[0].CopyToAsync(fileStream).ConfigureAwait(false);
                    fileStream.Close();
                }
            }

            return Json("file uploaded successfully");
        }
public static void AddImage(string file, string image, long width, long height)
        {
            using (var presentation = PresentationDocument.Open(file, true))
            {
                var slidePart = presentation
                    .PresentationPart
                    .SlideParts
                    .ElementAt(u);
                //.First();

                var part = slidePart
                    .AddImagePart(ImagePartType.Png);

                using (var stream = File.OpenRead(image))
                {
                    // try catch weil es sonst einen "process is using pic" error gibt - UNSOLVED
                    try
                    {
                        part.FeedData(stream);
                        //stream.Flush();
                        //stream.Dispose();
                        stream.Close();
                    }
                    catch
                    {
                        stream.Close();
                    }
                }
                var tree = slidePart
                    .Slide
                    .Descendants<DocumentFormat.OpenXml.Presentation.ShapeTree>()
                    .First();

                var picture = new DocumentFormat.OpenXml.Presentation.Picture();

                picture.NonVisualPictureProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureProperties();
                picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.NonVisualDrawingProperties
                {
                    Name = "My Shape",
                    Id = (UInt32)tree.ChildElements.Count - 1
                });

                var nonVisualPictureDrawingProperties = new DocumentFormat.OpenXml.Presentation.NonVisualPictureDrawingProperties();
                nonVisualPictureDrawingProperties.Append(new DocumentFormat.OpenXml.Drawing.PictureLocks()
                {
                    NoChangeAspect = true
                });
                picture.NonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
                picture.NonVisualPictureProperties.Append(new DocumentFormat.OpenXml.Presentation.ApplicationNonVisualDrawingProperties());

                var blipFill = new DocumentFormat.OpenXml.Presentation.BlipFill();
                var blip1 = new DocumentFormat.OpenXml.Drawing.Blip()
                {
                    Embed = slidePart.GetIdOfPart(part)
                };
                var blipExtensionList1 = new DocumentFormat.OpenXml.Drawing.BlipExtensionList();
                var blipExtension1 = new DocumentFormat.OpenXml.Drawing.BlipExtension()
                {
                    Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"
                };
                var useLocalDpi1 = new DocumentFormat.OpenXml.Office2010.Drawing.UseLocalDpi()
                {
                    Val = false
                };
                useLocalDpi1.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");
                blipExtension1.Append(useLocalDpi1);
                blipExtensionList1.Append(blipExtension1);
                blip1.Append(blipExtensionList1);
                var stretch = new DocumentFormat.OpenXml.Drawing.Stretch();
                stretch.Append(new DocumentFormat.OpenXml.Drawing.FillRectangle());
                blipFill.Append(blip1);
                blipFill.Append(stretch);
                picture.Append(blipFill);

                picture.ShapeProperties = new DocumentFormat.OpenXml.Presentation.ShapeProperties();
                picture.ShapeProperties.Transform2D = new DocumentFormat.OpenXml.Drawing.Transform2D();
                picture.ShapeProperties.Transform2D.Append(new DocumentFormat.OpenXml.Drawing.Offset
                {
                    X = 9144000/2 - width/2,
                    Y = 6800000/2 - height/2,
                });
                picture.ShapeProperties.Transform2D.Append(new DocumentFormat.OpenXml.Drawing.Extents
                {
                    Cx = (int) width,
                    Cy = (int) height,
                });
                picture.ShapeProperties.Append(new DocumentFormat.OpenXml.Drawing.PresetGeometry
                {
                    Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle
                });

                tree.Append(picture);
            }
}


Viewing all articles
Browse latest Browse all 9386

Trending Articles