正在上传照片(因为正在被另一个进程使用)。

时间:2010-06-24 10:55:52

标签: asp.net

当我上传图片时,它显示此错误,请你帮我吗?

该进程无法访问文件'C:\ Websites \ telugufilmchance \ httpdocs \ User \ photo \ thumb \ PPTS00025sonali.jpeg',因为它正由另一个进程使用。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.IO.IOException:进程无法访问文件'C:\ Websites \ telugufilmchance \ httpdocs \ User \ photo \ thumb \ PPTS00025sonali.jpeg',因为它正由另一个进程使用。

来源错误:

if (ext == ".JPEG" || ext == ".JPG" || ext == ".PNG" || ext == ".BMP" || ext == ".GIF")
Line 193:            {
Line 194:                fupPhoto.PostedFile.SaveAs(path + filename);
Line 195:                Bitmap src = Bitmap.FromStream(fupPhoto.PostedFile.InputStream) as Bitmap;
Line 196:                Bitmap result = ResizeBitmap(src);

这是我的代码(更新代码,它是一个编辑页面,我正在更新)

  if (fupPhoto.FileName != string.Empty)
        {
            filename = ran(fupPhoto.FileName);
            FileInfo fi = new FileInfo(filename);
            string ext = fi.Extension.ToUpper();
            string path = Server.MapPath("../User/photo/thumb/");              
            if (ext == ".JPEG" || ext == ".JPG" || ext == ".PNG" || ext == ".BMP" || ext == ".GIF")
            {
                fupPhoto.PostedFile.SaveAs(path + filename);
                Bitmap src = Bitmap.FromStream(fupPhoto.PostedFile.InputStream) as Bitmap;
                Bitmap result = ResizeBitmap(src);
                SizeMgt(filename);
                System.Drawing.Bitmap img = new System.Drawing.Bitmap(result, newwid, newhgt);
                System.Drawing.Imaging.ImageCodecInfo jpegcodec = null;
                System.Drawing.Imaging.EncoderParameters EncParams;
                foreach (System.Drawing.Imaging.ImageCodecInfo codec in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
                {
                    if (codec.MimeType == "image/jpeg")
                    {
                        jpegcodec = codec;
                        break;
                    }
                }

                EncParams = new System.Drawing.Imaging.EncoderParameters(1);
                EncParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 20L);
                newfilename = "new" + filename;
                img.Save(path + newfilename, jpegcodec, EncParams);
                img.Dispose();

            }
        }
        else
        {
            newfilename = myphot; 
        }

                cmd = new SqlCommand("sp_uptreg", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@tregno", lblregno1.Text);
                cmd.Parameters.AddWithValue("@catid", ddlCategory.SelectedItem.Value);

                cmd.Parameters.AddWithValue("@fname", txtfname.Text);
                cmd.Parameters.AddWithValue("@mname", txtmname.Text);
                cmd.Parameters.AddWithValue("@lname", txtlname.Text);

                cmd.Parameters.AddWithValue("@emailid", txtEmail.Text);
                cmd.Parameters.AddWithValue("@phoneno", txtphone.Text);
                cmd.Parameters.AddWithValue("@mobileno", txtMobile.Text);
                cmd.Parameters.AddWithValue("@address", txtAdd.Text);

                cmd.Parameters.AddWithValue("@photos", newfilename);


                    int a = cmd.ExecuteNonQuery();

2 个答案:

答案 0 :(得分:1)

我会说你保存文件的代码没有处理你持有的所有句柄,所以当你试图再次保存它时,它已经被使用了。你能发布保存代码吗?

答案 1 :(得分:0)

如果文件全部存在,我认为这个函数失败了!

 fupPhoto.PostedFile.SaveAs(path + filename);

所以你需要先删除它(如果存在)。

您可以使用FileInfo(路径+文件名),查看是否有FileExist并将其删除。

从SaveAs,您会看到具有“创建选项”。如果文件全部存在,则该过程失败。

public void SaveAs(string filename)
{
    if (!Path.IsPathRooted(filename) && RuntimeConfig.GetConfig().HttpRuntime.RequireRootedSaveAsPath)
    {
        throw new HttpException(SR.GetString("SaveAs_requires_rooted_path", new object[] { filename }));
    }
    FileStream s = new FileStream(filename, FileMode.Create);
    try
    {
        this._stream.WriteTo(s);
        s.Flush();
    }
    finally
    {
        s.Close();
    }
}

我建议在需要处理的对象上使用使用关键字

        using (Bitmap src = Bitmap.FromStream(fupPhoto.PostedFile.InputStream) as Bitmap)
        {
            //Bitmap result = ResizeBitmap(src);
            //SizeMgt(filename);                
            using (System.Drawing.Bitmap img = new System.Drawing.Bitmap(result, newwid, newhgt))
            {
                System.Drawing.Imaging.ImageCodecInfo jpegcodec = null;
                System.Drawing.Imaging.EncoderParameters EncParams;
                foreach (System.Drawing.Imaging.ImageCodecInfo codec in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
                {
                    if (codec.MimeType == "image/jpeg")
                    {
                        jpegcodec = codec;
                        break;
                    }
                }

                EncParams = new System.Drawing.Imaging.EncoderParameters(1);
                EncParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 20L);
                newfilename = "new" + filename;
                img.Save(path + newfilename, jpegcodec, EncParams);
                img.Dispose();
            }
            src.Dispose();
        }