uses JS + form form post and get to submit data


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();

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

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. = "hidden_frame";
      var pos = _file.nextSibling;

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;
        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();
           // Remember to save it to the server where the application is published!
      //... Other code


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.

Recommended Today

Query SAP multiple database table sizes

Query SAP multiple database table sizes Item code db02 Here are two approaches, In the first graphical interface, the results of the query data table are displayed in MB, and only one table can be queried at a time. SPACE—Segments—Detailed Analysis—Detailed Analysis  In the pop-up window segment / object, enter the name of the […]