[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

Request parameters after submit

In my code given below onclicking the upload button i want to validate if the file name is what i want it to be and and then upload.If i call submit() onclick of the upload button I am able to retain the  parameters using request.getParameter but no validation is happenin and if I use some other function name for eg onclick of upload button if i call page() then the validation happens but the parameters using request.getParameter are not retained could u please help me correct my code so that m able to do the validation and retain the required parameters using request.getParameter and finally upload the file.Thanks in advance
<%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%@ page import="java.io.*,javax.servlet.http.HttpServletRequest,javax.servlet.ServletInputStream" %>  
<%@ page import="java.io.FileWriter,java.io.IOException" %> 
<%
	String strfilename = (String)request.getParameter("filename");
	if(strfilename==null)strfilename="";
	String stroriginalfilepath = (String)request.getParameter("originalfilepath");
	if(stroriginalfilepath==null)stroriginalfilepath="";
	String strcopyfilepath = (String)request.getParameter("copyfilepath");
	if(strcopyfilepath==null)strcopyfilepath="";
	String strMode = (String)request.getParameter("Mode");
	if(strMode==null)strMode="";

	//out.println("New "+strfilename+" "+stroriginalfilepath+" "+strcopyfilepath);

	String savePath = "", filepath = "", filename = "";  
	String contentType = "", fileData = "", strLocalFileName = "";  
	int startPos = 0, endPos = 0;  
	int BOF = 0, EOF = 0;  
%>  
<%!  
	//copy specified number of bytes from main data buffer to temp data buffer  
	void copyByte(byte [] fromBytes, byte [] toBytes, int start, int len)  
	{  
		for(int i=start;i<(start+len);i++)  
		{  
			toBytes[i-start] = fromBytes[i]; 
		}  
	}  
%>  
<%	

	contentType = request.getContentType();  
	//out.println("<br>Content type is :: " +contentType);  
	if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))   
	{  
		DataInputStream in = new DataInputStream(request.getInputStream());  
		DataInputStream in1 = in;  
		int formDataLength = request.getContentLength();  
		byte dataBytes[] = new byte[formDataLength];  
		int byteRead = 0;  
		int totalBytesRead = 0;  
		while (totalBytesRead < formDataLength)  
		{	  
			byteRead = in1.read(dataBytes, totalBytesRead, formDataLength);  
			totalBytesRead += byteRead;  
		}  
		//out.println("<br>totalBytesRead : " + totalBytesRead + "    :   formDataLength = " + formDataLength);  
		  
		//String file = new String(dataBytes);   
		//out.println("<br>File Contents:<br>////////////////////////////////////<br>" + file + "<br>////////////////////////////////<br>");  
  
		byte[] line = new byte[128];  
		if (totalBytesRead < 3)	  
		{  
		  return;	//exit if file length is not sufficiently large  
		}  
  
		String boundary = "";  
		String s = "";  
		int count = 0;		  
		int pos = 0;  
		  
		//loop for extracting boundry of file  
		//could also be extracted from request.getContentType()  
		do  
		{  
			copyByte(dataBytes, line, count ,1);	//read 1 byte at a time  
			count+=1;  
			s = new String(line, 0, 1);  
			fileData = fileData + s;  
			pos = fileData.indexOf("Content-Disposition: form-data; name=\""); //set the file name  
			if(pos != -1)  
				endPos = pos;  
		}while(pos == -1);  
		boundary = fileData.substring(startPos, endPos);  
  
		//loop for extracting filename  
		startPos = endPos;  
		do  
		{  
			copyByte(dataBytes, line, count ,1);	//read 1 byte at a time  
			count+=1;  
			s = new String(line, 0, 1);  
			fileData = fileData + s;  
			pos = fileData.indexOf("filename=\"", startPos); //set the file name  
			if(pos != -1)  
				startPos = pos;  
		}while(pos == -1);					  
		do  
		{  
			copyByte(dataBytes, line, count ,1);	//read 1 byte at a time  
			count+=1;  
			s = new String(line, 0, 1);  
			fileData = fileData + s;  
			pos = fileData.indexOf("Content-Type: ", startPos);  
			if(pos != -1)  
				endPos = pos;						  
		}while(pos == -1);  
		filename = fileData.substring(startPos + 10, endPos - 3);	//to eliminate " from start & end  
		strLocalFileName = filename;  
		int index = filename.lastIndexOf("\\");  
		if(index != -1)  
			filename = filename.substring(index + 1);  
		else  
			filename = filename;  
		  
		//loop for extracting ContentType  
		boolean blnNewlnFlag = false;  
		startPos = endPos;	//added length of "Content-Type: "  
		do  
		{  
			copyByte(dataBytes, line, count ,1);	//read 1 byte at a time  
			count+=1;  
			s = new String(line, 0, 1);  
			fileData = fileData + s;			  
			pos = fileData.indexOf("\n", startPos);  
			if(pos != -1)  
			{  
				if(blnNewlnFlag == true)  
					endPos = pos;					  
				else  
				{  
					blnNewlnFlag = true;  
					pos = -1;  
				}  
			}  
		}while(pos == -1);  
		contentType = fileData.substring(startPos + 14, endPos);  
		  
		//loop for extracting actual file data (any type of file)  
		BOF = count + 1;  
		do  
		{  
			copyByte(dataBytes, line, count ,1);	//read 1 byte at a time  
			count+=1;  
			s = new String(line, 0, 1);  
			fileData = fileData + s;  
			pos = fileData.indexOf(boundary, startPos);	//check for end of file data i.e boundry value			  
		}while(pos == -1);  
		EOF = count - boundary.length();  
		//file data extracted  
  
		/*out.println("<br><br>0. Local File Name = " + strLocalFileName);  
		out.println("<br><br>1. filename = " + filename);  
		out.println("<br>2. contentType = " + contentType);  
		out.println("<br>3. startPos = " + BOF);  
		out.println("<br>4. endPos = " + EOF);  
		out.println("<br>5. boundary = " + boundary);*/  
  
		//create destination path & save file there  
		String appPath = application.getRealPath("/");  
		//out.println("<br>appPath : " + appPath);  
		String destFolder = stroriginalfilepath;	//change this as required  
		filename= destFolder + filename; 
		System.out.println("Filename "+filename);
		FileOutputStream fileOut = new FileOutputStream(filename);  
		fileOut.write(dataBytes, BOF, (EOF - BOF));  
		fileOut.flush();  
		fileOut.close();  
		out.println("<br>File saved as >> " + filename);		  
		//file saved at destination  
		//out.println("<br>File data : <br><br>**************************<br>" + (new String(dataBytes,startPos, (endPos - startPos))) + "<br><br>**************************");  
	}  
	else  
	{  
		out.println("");  
	}  
 
%>  
<html>
<body>
<form method="post" name="upform" enctype="multipart/form-data">  
  <table width="60%" border="0" cellspacing="1" cellpadding="1" align="center" class="style1">  
    <tr>  
      <td align="left"><b>Select a file to upload :</b></td>  
    </tr>  
    <tr>  
      <td align="left">  
        <input type="file" name="uploadfile" size="50">  
        </td>  
    </tr>  
    <tr>  
      <td align="left">  
		<input type="hidden" name="todo" value="upload">  
        <input type="button" name="upload" value="Upload" onclick="submit()">  
        <input type="reset" name="Reset" value="Cancel">  
        </td>  
    </tr>  
  </table> 

<input type="hidden" name="filename" value="<%=strfilename%>"/>
<input type="hidden" name="originalfilepath" value="<%=stroriginalfilepath%>"/>
<input type="hidden" name="copyfilepath" value="<%=strcopyfilepath%>"/>
<input type="hidden" name="Mode" value=<%=strMode%> />

</form>  
</body>  
</html>
<script type="text/javascript">
function submit()
{
	var originalFilename = document.upform.filename.value;
	var strPath = document.getElementsByName("uploadfile")[0].value;
	var objRE = new RegExp(/([^\\]+)$/);
	var fileName = objRE.exec(strPath);
	alert(fileName[0]);
	alert(originalFilename);

	if(fileName[0]==originalFilename)
	{
		document.upform.Mode.value="Modify";
		document.upform.action="uploadCtlScan.jsp";
		document.upform.submit();
	}
	else
	{
		alert("Please Upload the original file");
	}
}
</script>

Open in new window

0
czutshi
Asked:
czutshi
  • 2
1 Solution
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
You shoud not use a system function -submit()- foryour custom js function name.

and better not calling directly from upload button because forms may also be submitted by Return key

Right approach is ;

1 - change your functions name  to something like form_check()

2- change this line
 <form method="post" name="upform" enctype="multipart/form-data">  
as
<form method="post" name="upform" enctype="multipart/form-data" onsubmit="return form_check();" > 


Now your form will not be sent unless your form_check function finishes his work and returns a true boolean .
dont forget to use

return false ;

where you want to block form submission ..

This usage controls both submit button and Return submissions.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Have you tried the solution i suggested ?
0
 
czutshiAuthor Commented:
pretty ok but was lookin for a better work around
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now