1

I am using c# mvc application to develop my website.

The following code gave me some problem, because I intend to trigger the upload after user select file to upload. So I apply on changes event on my file upload.

<form style="display:inline;" enctype="multipart/form-data" action="/Admin/AddImage" method="post" id="uploadImageForm">
  <input hidden name="listing_id" value="@Request.QueryString["id"]"/>
  <label for="file-upload" style="width :120px" class="btn btn-default custom-file-upload">
    <input name="uploaded_image" id="file-upload" type="file" onchange="document.getElementById('uploadImageForm').submit();" />
           Add Image
  </label>
</form>

Ok this works fine. However when I click back button on my chrome, this stupid event will trigger again so become upload twice. How I can prevent it to be happen.

The problem is, when i click back, the event will trigger when bodyonload. so the input file have the cache, so it will trigger another upload

ryan1555
  • 165
  • 10
  • You could redirect them after submit to every page you want for c# is the code: `Response.Redirect("http://www.example.com")` – J.vee Dec 05 '18 at 12:35
  • @J.vee it is a image upload function, i cannot identify it is a back request or normal request – ryan1555 Dec 05 '18 at 12:38
  • maybe this [link](https://stackoverflow.com/questions/11063900/determine-if-uploaded-file-is-image-any-format-on-mvc) will help you further – J.vee Dec 05 '18 at 12:39
  • Pls also take a look at this [link](https://stackoverflow.com/questions/37511043/how-to-stop-re-submitting-a-form-after-clicking-back-button) – J.vee Dec 05 '18 at 12:41

1 Answers1

0

Client Side : You Can Add in Client Side Using Restrict the user from using the browser back button by removing the page from the history.

history.pushState(null, null, document.title);
window.addEventListener('popstate', function () {
    history.pushState(null, null, document.title);
});