title: How can I save images in a folder with javascript? ASP.NET
body: <p>with fileupload in asp.net, how can I save an image in a folder and then call and display it? Can I use ajax, jquery or javascript with web method?</p>
  
  <pre><code>&lt;asp:FileUpload CssClass="image" ID="fileUpload" runat="server" /&gt;
  </code></pre>
  
  <p>I have these methods in c #, but I need it in javascript.</p>
  
  <pre><code> private void SaveFile(HttpPostedFile file)
          {
              string rut = Server.MapPath("~/temp");
  
              if (!Directory.Exists(rut))
              {
                  Directory.CreateDirectory(rut);
              }
  
              string imgff= String.Format("{0}\\{1}", rut, file.FileName);
  
              if (File.Exists(imgff))
              {
                  ClientScript.RegisterStartupScript(this.GetType(), "myalert", "Image()", true);
              }
              else
              {
                  file.SaveAs(imgff);
              }
          }
  </code></pre>
  
  <p>With this method:</p>
  
  <pre><code>private void carga()
          {
              try
              {
                  if (fileUpload.HasFile)
                  {
                      // Se verifica que la extensión sea de un formato válido
                      string ext = fileUpload.PostedFile.FileName;
                      ext = ext.Substring(ext.LastIndexOf(".") + 1).ToLower();
                      string[] formatos =
                        new string[] { "jpg", "jpeg", "bmp", "png" };
                      if (Array.IndexOf(formatos, ext) &lt; 0)
                      {
                          ClientScript.RegisterStartupScript(this.GetType(), "myalert", "FormatoImagen()", true);
                      }
                      else
                      {
                          GuardarArchivo(fileUpload.PostedFile);
                      }
                  }
              }
              catch (Exception ex)
              {
                  throw ex;
              }
          }
  </code></pre>