This enctype is already there. The problem is that only one image is passed through the code! than the Response.Redirect line runs and redirects the user. While the code should run for every image that is to be uploaded.
Mikesdotnetting
Other than that, your code should work as it does with the fileupload helper.
Ok, if any fileupload helper will work. Can I use the method provided in the files menu for the images type. Because when I use <input> it needs
This is used. So I was asking for the Request file count method.
Is there another method for counting the files in input? Or can I simply try using like anything
var image_Get = Request.Form["images"];
var image_Count = image_Get.Count();
// In the page
<input type="file" name="images" />
// I let the other form elements go because I want to highlight the input type and its name here..Only!
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
But the issue is that I have used <input type="file" multiple="multiple" /> So now I can upload two or more files too. But the code that I am using only saves the first file. And only inserts into the first file code. Than redirects the
user.
I want to upload number of files in one time! This is the scenario of my question this time. No error is there in it. But the main issue is that I want to loop INSERT INTO statement and SAVE code. For every file the user selects.
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Working with images not files.
Dec 18, 2012 12:53 PM|LINK
@{ var message = ""; if (IsPost) { var fileName = ""; var fileSavePath = ""; int numFiles = Request.Files.Count; int uploadedCount = 0; for(int i =0; i < numFiles; i++) { var uploadedFile = Request.Files[i]; if (uploadedFile.ContentLength > 0) { fileName = Path.GetFileName(uploadedFile.FileName); fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" + fileName); uploadedFile.SaveAs(fileSavePath); uploadedCount++; } } message = "File upload complete. Total files uploaded: " + uploadedCount.ToString(); } }@if (!IsPost) { @FileUpload.GetHtml( initialNumberOfFiles:2, allowMoreFilesToBeAdded:true, includeFormTag:true, addText:"Add another file", uploadText:"Upload") }This code is being displayed on this page : http://www.asp.net/web-pages/tutorials/files,-images,-and-media/working-with-files
My question in this one is that. For this page. I mean the above one
@if (!IsPost) { @FileUpload.GetHtml( initialNumberOfFiles:2, allowMoreFilesToBeAdded:true, includeFormTag:true, addText:"Add another file", uploadText:"Upload") }This upload type is used. I want to use <input type="file" />
And for that type the server side is totally inverse. So what would be the type to get the int for loop.
Actually I want to INSERT INTO and SAVE multiple files. I have tried but without loops only first file is saved in FILES and in DATABASE.
So I wanted to learn uploading multiple files and for each of image I would loop the same code and save them.
How to count the file?
int numFiles = Request.Files.Count;This aint gonna do the job according to me. Because input wont give it hold of it.
~~! FIREWALL !~~
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Working with images not files.
Dec 18, 2012 01:25 PM|LINK
You must make sure that your form tag includes an enctype attribute with a value of multipart/form-data:
Other than that, your code should work as it does with the fileupload helper.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Working with images not files.
Dec 18, 2012 04:19 PM|LINK
This enctype is already there. The problem is that only one image is passed through the code! than the Response.Redirect line runs and redirects the user. While the code should run for every image that is to be uploaded.
Ok, if any fileupload helper will work. Can I use the method provided in the files menu for the images type. Because when I use <input> it needs
@{ WebImage photo = null; var newFileName = ""; var imagePath = ""; if(IsPost){ photo = WebImage.GetImageFromRequest(); if(photo != null){ newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo.FileName); imagePath = @"images\" + newFileName; photo.Save(@"~\" + imagePath); } } }photo = WebImage.GetImageFromRequest();
While in files
This is used. So I was asking for the Request file count method.
Is there another method for counting the files in input? Or can I simply try using like anything
~~! FIREWALL !~~
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Working with images not files.
Dec 18, 2012 05:27 PM|LINK
I don't see that in your original code. Are you actually using the code you posted?
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Working with images not files.
Dec 19, 2012 08:33 AM|LINK
No no actually the code I am using is taken from this website. the code that I am using contains some SQL statements too. Here
@{ WebImage photo = null; var newFileName = ""; var imagePath = ""; if(IsPost){ photo = WebImage.GetImageFromRequest(); if(photo != null){ newFileName = Guid.NewGuid().ToString() + "_" + Path.GetFileName(photo.FileName); imagePath = @"User\UploadedFiles\" + newFileName; photo.Save(@"~\" + imagePath); } var Message = ""; if(IsPost){ Message = Request.Form["message"]; var fileName = newFileName; var insertQuery = "INSERT INTO Status_Post (UserId, Message, TimeOfPost, Status_From, Image_Name, Profile_Pic_Update, Image_Present) VALUES (@0, @1, @2, @3, @4, @5, @6)"; var insertQuery2 = "INSERT INTO Albums_Data (Album_Id, Time, Source, Message, Album_UserId, From_Name, Is_Profile_Pic) VALUES (@0, @1, @2, @3, @4, @5, @6)"; db2.Execute(insertQuery, myId, Message, Time, myName, fileName, 0, 1); db2.Execute(insertQuery2, "1", Time, fileName, Message, myId, myName, "False"); Response.Redirect("~/"); } } }This is working fine.
But what if I have to upload multiple files at once?
This is in <body>
<form action="" method="post" enctype="multipart/form-data"> <fieldset> <legend> Upload Image </legend> <label for="Image">Image</label><br> <textarea placeholder="Its better to add some description to Image or to explain your mood.." style="border: 1px solid #666;padding: 3px;" name="message" rows="3" cols="75" ></textarea><br> <input type="file" accept="Image/*" name="Image" /> <br/> <select name="privacy"> <option value="0">No one</option> <option value="1" selected>Frends</option> <option value="2">Everyone</option> </select> <br> <input type="submit" value="Upload"/> </fieldset> </form>~~! FIREWALL !~~
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Working with images not files.
Dec 19, 2012 08:46 AM|LINK
Use the code you posted in your thread starter. But you also need multiple <input type="file">.
By the way, images are files. You treat them in exactly the same way )as far as an upload goes) as you would any other type of file.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Working with images not files.
Dec 19, 2012 01:43 PM|LINK
Thanks for answer:)
But will that Request.Files.Count; work with <input type="file" /> too?
Because it says something like index was not in correct format. Something like that!
~~! FIREWALL !~~
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Working with images not files.
Dec 19, 2012 05:27 PM|LINK
Have you tried some code? If so, paste it here with the exact error message that you get.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Working with images not files.
Dec 19, 2012 06:26 PM|LINK
I am using this code! Its error-less.
But the issue is that I have used <input type="file" multiple="multiple" /> So now I can upload two or more files too. But the code that I am using only saves the first file. And only inserts into the first file code. Than redirects the user.
I want to upload number of files in one time! This is the scenario of my question this time. No error is there in it. But the main issue is that I want to loop INSERT INTO statement and SAVE code. For every file the user selects.
~~! FIREWALL !~~
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Working with images not files.
Dec 19, 2012 08:43 PM|LINK
As I said before, you should use the code you first posted to loop through the files.
for(var i = 0; i < Request.Files.Count; i++){
//process the file at Request.Files[i]
}
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter