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

后端 未结 3 584
陌清茗
陌清茗 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.

提交回复
热议问题