Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Uploading files

Uploading files

Scheduled Pinned Locked Moved ASP.NET
csharpquestionc++asp-netsysadmin
7 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rickard Andersson20
    wrote on last edited by
    #1

    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++!

    P 1 Reply Last reply
    0
    • R Rickard Andersson20

      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++!

      P Offline
      P Offline
      Paul Watson
      wrote on last edited by
      #2

      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 or bitmap instance and check the width and height there.

      Paul Watson
      Bluegrass
      Cape Town, South Africa

      My photoSIG portfolio[^]

      R 1 Reply Last reply
      0
      • P Paul Watson

        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 or bitmap instance and check the width and height there.

        Paul Watson
        Bluegrass
        Cape Town, South Africa

        My photoSIG portfolio[^]

        R Offline
        R Offline
        Rickard Andersson20
        wrote on last edited by
        #3

        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 Fileclass? 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++!

        L S 2 Replies Last reply
        0
        • R Rickard Andersson20

          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 Fileclass? 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++!

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          I would load an Image strait from the incoming stream then only saving it if it's big enough. Who is this miscrosoft, and what devilish plans have they for us?

          1 Reply Last reply
          0
          • R Rickard Andersson20

            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 Fileclass? 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++!

            S Offline
            S Offline
            stephen woolhead
            wrote on last edited by
            #5

            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.

            R 1 Reply Last reply
            0
            • S stephen woolhead

              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.

              R Offline
              R Offline
              Rickard Andersson20
              wrote on last edited by
              #6

              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>

              S 1 Reply Last reply
              0
              • R Rickard Andersson20

                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>

                S Offline
                S Offline
                stephen woolhead
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups