Uploading files
-
When the user of my homepage is uploading an image file to the server, I want to check the height and width of the image before he can save it to the server. How can I do that? Do I have to do client-side stuff, or is it possible in ASP.NET? Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!
-
When the user of my homepage is uploading an image file to the server, I want to check the height and width of the image before he can save it to the server. How can I do that? Do I have to do client-side stuff, or is it possible in ASP.NET? Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!
Rickard Andersson wrote: I want to check the height and width of the image before he can save it to the server. How can I do that? Technically yes you can check before you upload, but then you need some client side ActiveX or Java controls to do it for you which the user would need to download. The best way is to upload the image to a holding folder, then open it as an
image
orbitmap
instance and check the width and height there.Paul Watson
Bluegrass
Cape Town, South Africa -
Rickard Andersson wrote: I want to check the height and width of the image before he can save it to the server. How can I do that? Technically yes you can check before you upload, but then you need some client side ActiveX or Java controls to do it for you which the user would need to download. The best way is to upload the image to a holding folder, then open it as an
image
orbitmap
instance and check the width and height there.Paul Watson
Bluegrass
Cape Town, South AfricaPaul Watson wrote: The best way is to upload the image to a holding folder, then open it as an image or bitmap instance and check the width and height there. Okay, but then I need to delete this file if it doesn't have the right width and height? Can I do it with the
File
class? Of course I it would be possible... hm... Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++! -
Paul Watson wrote: The best way is to upload the image to a holding folder, then open it as an image or bitmap instance and check the width and height there. Okay, but then I need to delete this file if it doesn't have the right width and height? Can I do it with the
File
class? Of course I it would be possible... hm... Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++! -
Paul Watson wrote: The best way is to upload the image to a holding folder, then open it as an image or bitmap instance and check the width and height there. Okay, but then I need to delete this file if it doesn't have the right width and height? Can I do it with the
File
class? Of course I it would be possible... hm... Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C#, ASP.NET and C++!Rickard Andersson wrote: Okay, but then I need to delete this file if it doesn't have the right width and height? Can I do it with the Fileclass? Of course I it would be possible... hm... You can do the whole process in memory if you want, even resize it if you feel like it with only a few lines of code. Stephen.
-
Rickard Andersson wrote: Okay, but then I need to delete this file if it doesn't have the right width and height? Can I do it with the Fileclass? Of course I it would be possible... hm... You can do the whole process in memory if you want, even resize it if you feel like it with only a few lines of code. Stephen.
stephen woolhead wrote: You can do the whole process in memory if you want Damn, how would I do then? I do like this to upload:
public void SaveImage(object sender, System.EventArgs e)
{
if (FileBrowse.PostedFile != null)
{
try
{
string fileName = "newname.jpg";
FileBrowse.PostedFile.SaveAs(fileName);
System.Drawing.Image pic = System.Drawing.Image.FromFile(Server.MapPath(fileName));
if((pic.Height != 105) | (pic.Width != 105))
{
Status.Text = "Bilden är inte 105x105!";
System.IO.File.Delete(Server.MapPath(fileName));
}
System.IO.File.Move(fileName, @"/Bilder/" + fileName);
Status.Text = "Din bild är nu uppdaterad!";
}
catch(Exception err)
{
Response.Write(err.ToString());
//Status.Text = "Bilden kunde inte laddas upp";
}
}
}</pre>I havn't tried this code so much, and I think the code didn't worked... hm.. perhaps because <code>FileBrowse</code> is an HTML element and not an ASP.NET thing? I perhaps need to <code>Page.Form["Formname"].FileBrowse</code> or something.. I don't know...
<font size="2" color="009933"><B>Rickard Andersson@Suza Computing</B></font><FONT face="Verdana"><FONT color="#000000" size="1">
<EM>C# and C++ programmer from SWEDEN!</EM></FONT>
<font face="Verdana"><small><B>UIN:</B> 50302279
<B>E-Mail:</B> <a href="mailto:nikado@pc.nu">nikado@pc.nu</a>
<B>Speciality: </B>I love C#, ASP.NET and C++!
</small></font></x-turndown> -
stephen woolhead wrote: You can do the whole process in memory if you want Damn, how would I do then? I do like this to upload:
public void SaveImage(object sender, System.EventArgs e)
{
if (FileBrowse.PostedFile != null)
{
try
{
string fileName = "newname.jpg";
FileBrowse.PostedFile.SaveAs(fileName);
System.Drawing.Image pic = System.Drawing.Image.FromFile(Server.MapPath(fileName));
if((pic.Height != 105) | (pic.Width != 105))
{
Status.Text = "Bilden är inte 105x105!";
System.IO.File.Delete(Server.MapPath(fileName));
}
System.IO.File.Move(fileName, @"/Bilder/" + fileName);
Status.Text = "Din bild är nu uppdaterad!";
}
catch(Exception err)
{
Response.Write(err.ToString());
//Status.Text = "Bilden kunde inte laddas upp";
}
}
}</pre>I havn't tried this code so much, and I think the code didn't worked... hm.. perhaps because <code>FileBrowse</code> is an HTML element and not an ASP.NET thing? I perhaps need to <code>Page.Form["Formname"].FileBrowse</code> or something.. I don't know...
<font size="2" color="009933"><B>Rickard Andersson@Suza Computing</B></font><FONT face="Verdana"><FONT color="#000000" size="1">
<EM>C# and C++ programmer from SWEDEN!</EM></FONT>
<font face="Verdana"><small><B>UIN:</B> 50302279
<B>E-Mail:</B> <a href="mailto:nikado@pc.nu">nikado@pc.nu</a>
<B>Speciality: </B>I love C#, ASP.NET and C++!
</small></font></x-turndown>I do something like this, add one of these to your form (looks like you already have)
protected System.Web.UI.HtmlControls.HtmlInputFile filMyFile;
Then when the page is posted back do something like...
Bitmap myBitmap = new Bitmap(filMyFile.PostedFile.InputStream); if (myBitmap.Height) { ........
Remember to put a check in there to make sure that a file has been posted etc... Stephen.