Asp.net uses JS + form form post and get to submit data

Time:2019-10-15

Recently, a method of uploading files by post using JS + form has been used in our work. The front desk uses Html input and JS mode to upload files to the server. The specific implementation of the code is as follows:

The front page uses the aspx page, uses the input tag, uses its file type; this tag does not use runat= “server”. It does not use server controls; an iframe tag needs to be added here. And hide; set a label. Used as a user to click the button; call the JS function:Uploadfun();

<div>
  <input type="file" name="FileUpLoad" style="width:140px;" />
  <a href= "javascript: void (0);" rel= "external nofollow" onclick= "Upload fun ()"> upload</a>
 <iframe name="hidden_frame" style="width:10%;display:none;"></iframe>
 </div>

The JS code is as follows:

function Uploadfun(){
      Var_file = document. getElementById ("FileUpLoad"); here is the ID of the input tag on the front page
      Var_form = document. createElenent ("form"); // Create a form
      Document. body. appendChild (_form);// Add a form
      _ form.encoding = multipart/form-data; //Using this encoding procedure, you can not limit the 2M size of the post form
      _ form.method= "post";//Use POST
      _ Form. action = ". / Service / FileSrv. aspx? Type = Client & CallFun = UploadFile"; // Use Get here to pass to the background Server code layer of the front page;
      // This is the project location in my work.
      _form.target = "hidden_frame";
      var pos = _file.nextSibling;
      _form.appendChild(_file);
      _form.submit();
      pos.parentNode.insertBefore(_file,pos);
      document.body.renoveChild(_form);
    }

C# Layer Code: This is what the action of the form in the JS code identifies. In the background CS code of FileSrv. aspx, we can usegetqueryMethods The parameters transmitted by Get mode were obtained.

In this example, the parameter Type = Client is a module identifier, and CallFun is a response function that indicates the CS code layer to call.UploadFile();

The code is as follows:

private void UploadFile()
    {
      //
      //... Other code
      //
      HttpFileCollection files = HttpContext.Current.Request.Files;
      if(files.Count>0)
      {
        Int lintTemp = files [0]. FileName. LastIndexOf ("."); get the file file path in the input tag;
        string lstrFileType = string.Empty;
        string lstrContentType = string.Empty;
        if(lintTemp!=-1 &&files[0].FileName.Length>lintTemp+1)
        {
          lstrFileType = files[0].FileName.Substring(lintTemp+1).ToUpper();
        }
        if(lstrFileType.ToUpper()=="JPG")
        {
          if(files[0].ContentLength<10485760)
          {
           // Remember to save it to the server where the application is published!
            files[0].SaveAs(Server.MapPath("~/Files/")+"JPG1."+files[0].FileName.Substring(files[0].FileName.LastIndexOf(".")));
          }
        }
      }
      //
      //... Other code
      //
    }

summary

Above is the whole content of this article. I hope the content of this article has some reference value for your study or work. Thank you for your support to developpaer. If you want to know more about it, please check the links below.