Home
ASP.Net WebForm Upload ONE AT A TIME
What a bleeding palaver!
ASP.Net - Web Forms - old school - nothing wrong with that.
I need to upload a bunch of images. There could be 1, there could (possibly) be 1,000. Now... we are at risk with these big numbers of timing out in so many many ways. Wouldn't it be better to upload each image one at a time?
<div>
<input type="file" multiple="multiple" id="pix" /><!-- note the multiple -->
<input type="button" onclick="DoUp()" value="Go" />
<br />
<div id="myShow"></div>
</div>
<script>
async function DoUp() { //NOTE the "async" - this allows us to do things one by one
var myShow = document.getElementById('myShow');
var pix = document.getElementById('pix').files; //gets an array of files from the file input
for (var i = 0; i < pix.length; i++) { //loop through the items in the file input
var formData = new FormData(); //create the kind of thing that's sent in a "POST" request
formData.append("pic", pix[i]); //stick on ONE of the images to the form's data
await fetch( //NOTE "await" - wait for the fetch to be done
'wait.aspx', // the URL where this fetch is going to
{
method: 'POST', //we are "POST"ing rather than the usual getting
body: formData //in the body put formData which includes the image
})
.then(resp => { //THEN once the fetch is done do this...
myShow.innerHTML = "Done " + (i + 1)//Show what we've done so far
})
}
}
</script>
protected void Page_Load(object sender, EventArgs e)
{
Thread.Sleep(3000); //Pause the reply to pretend it was a long upload
HttpPostedFile x = Request.Files[0]; //Get the posted file note the [0]
x.SaveAs(Server.MapPath("/Uploads/NewPics") + "/" + x.FileName);//Save the file somewhere
}
Reader's Comments
Name
Comment
Add a RELEVANT link (not required)
Upload an image (not required)
Uploading...
Home