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

后端 未结 3 580
陌清茗
陌清茗 2021-01-24 22:21

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 imag

相关标签:
3条回答
  • 2021-01-24 22:41

    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.

    0 讨论(0)
  • 2021-01-24 22:48

    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.

    0 讨论(0)
  • 2021-01-24 22:54
    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 讨论(0)
提交回复
热议问题