开发者

overwrite files (image) and delete image in c#?

I have a simple form which the users can use to upload images. It has a preview button so when User selects an image and clicks on preview, a postback occurs and I save the image to temporary folder and resize it and show it on the page. from there, the user can either submit form or edit the form. If he submits, everything is okay, I copy the resized image into correct folder.

If he clicks edit, and chooses another image, I need to delete the uploaded files (both original and resized) and If I do this:

File.Delete(HostingEnvironment.MapPath(TmpDirectory + PostImageName + ".jpg"));
File.Delete(HostingEnvironment.MapPath(TmpDirectory + PostImageName + "_small_.jpg"));

I get an exception saying some other process is using the image 开发者_JAVA技巧and It cant delete. (even after postback!)

and Also, if, instead of deleting that file, I try to save the new image with the same name so that it would overwrite, it still throws the error because file already exists.

There is no limit to the number of times the user can edit / preview so I cant save files incrementally (it doesn't even make sense to do this)

Also, after the postback, when the user goes back to edit mode and views the form the file upload control is empty. how can I get the file upload control to retain the value? all the other textboxes and text areas and checkboxes behave properly.

to summarize these are my questions:

1) how on earth (or rather, in c#) can I delete files without getting that exception?

OR

1) how to overwrite files?

2) how to make the file upload control retain its value between postbacks.

Thanks.


The exception talks for itself : Your file is still being used/opened by another process, i.e. thread, that you have launched. I bet it is the process by which you open the image file for reading. Make sure you have closed the relevant stream, ten bucks that it will solve your issue.


Why are you using a Bitmap to open the file on the server side? You should simply stream the "file" using

Response.TransmitFile(physicalFileNameAndPath).

Where TransmitFile expects (as a string) the physical file path and name.

The problem stems from the the way you send the file to the client. The file is still "locked" by the previous step. If you fix this, you'll be able to delete the file if needed.

I've not used the FileUpload control but it looks like you're using ViewState (which is what helps retain the values in the fields after postback. Either the FileUpload control does not support ViewState or you've not configured it to use ViewState.

you could assign the required property of the FileUpload control with the correct "value" so it shows what you've expect. This would mean that you'd have to capture the said value when the form is posted and simply re-assign the value back to the FileUpload control's property during the DataBind.


  1. Make sure that your current process doesn't have a write handle open to the files in question
  2. If there are no handles then try turning off your virus scanner; an overly aggressive virus scanner could be taking a lock on those files just after they have been created. This might be preventing you from deleting the files.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜