首页 > 解决方案 > 如何将用户上传的图像保存到我以后可以访问的临时文件中,但之后会被删除

问题描述

我创建了一个图片上传器,供用户上传/裁剪他们的个人资料图片。当用户上传文件时,它会被保存到一个文件中,然后他们可以立即访问该文件以对其进行裁剪。

我尝试过使用 Path.GetTempPath() 和 Path.GetTempFileName(),但由于某种原因,我的裁剪器无法找到文件位置。非常感谢任何建议。

更新:所以我将 File.Delete(filePath) 添加到 btnCropClick 的末尾,但我收到一条错误消息,指出进程无法访问该文件,因为它正在被另一个进程使用。如何“释放”该文件以立即删除?

下面是用户上传他们选择的原始图像的代码

protected void btnUploadClick(object sender, EventArgs e)
    {
        //Upload Original Image Here
        String UploadFileName = "";
        String UploadFilePath = "";

        if (fileUploader.HasFile)
        {
            String ext = Path.GetExtension(fileUploader.FileName).ToLower();
            if (ext == ".jpg" || ext == ".jpeg" || ext == ".png")
            {
                UploadFileName = "orig_" + Guid.NewGuid().ToString() + ext;
                UploadFilePath = Path.Combine(Server.MapPath("images/OriginalImages"), UploadFileName);
                try
                {
                    fileUploader.SaveAs(UploadFilePath); //TODO: Need to make this a temp file that gets "destroyed" later

                    imgUpload.ImageUrl = "images/OriginalImages/" + UploadFileName;
                    panCrop.Visible = true;
                }
                catch (Exception ex)
                {
                    lblMsg.Text = "Error! Please Try Again. ";
                }
            }
            else
            {
                lblMsg.Text = "Invalid File Type Selected. | Please Choose .jpg, .jpeg, or .png file only.";
            }
        }
        else
        {
            lblMsg.Text = "Please Click 'Choose File' & Select An Image To Upload";
        }
    }

这是裁剪器的代码(不确定此处是否需要更改,但无论如何我都会将其包括在内以用于上下文和相关性

    protected void btnCropClick(object sender, EventArgs e)
    {
        //Crop Image Here & Save
        String fileName = Path.GetFileName(imgUpload.ImageUrl);
        String filePath = Path.Combine(Server.MapPath("images/OriginalImages"), fileName);
        String cropFileName = "";
        String cropFilePath = "";

        if (File.Exists(filePath))
        {
            System.Drawing.Image orgImg = System.Drawing.Image.FromFile(filePath);

            Rectangle CropArea = new Rectangle(
                Convert.ToInt32(X.Value),
                Convert.ToInt32(Y.Value),
                Convert.ToInt32(W.Value),
                Convert.ToInt32(H.Value)
                );

            try
            {
                Bitmap bitMap = new Bitmap(CropArea.Width, CropArea.Height);
                using (Graphics g = Graphics.FromImage(bitMap))
                {
                    g.DrawImage(orgImg, new Rectangle(0, 0, bitMap.Width, bitMap.Height), CropArea, GraphicsUnit.Pixel);

                    Bitmap resized = new Bitmap(bitMap, new Size(200, 200)); //Resize image to save as 200x200

                    cropFileName = "crop_" + fileName; //+UserID so each fileName is unique
                    cropFilePath = Path.Combine(Server.MapPath("images/CroppedImages"), cropFileName);
                    resized.Save(cropFilePath); //Where final, cropped image is saved
                    imgHeadshot.ImageUrl = "images/CroppedImages/" + cropFileName;
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            panCrop.Visible = false;
        }
    }

标签: c#asp.net

解决方案


能够通过添加以下内容来处理原始文件:

orgImg.Dispose();
bitMap.Dispose();
File.Delete(filePath);

到 btnCropClick 方法的末尾。


推荐阅读