1. 程式人生 > >html form表單提交資料並後臺獲取

html form表單提交資料並後臺獲取

http://www.tuicool.com/articles/m67vMbQ

前臺:

HTML的程式碼:(關鍵是要在form裡設定enctype="multipart/form-data",這樣才能在提交表單時,將檔案以二進位制流的形式傳輸到伺服器)

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
<title>無標題頁</title> 
</head> 
<body> 
<form id="Form1" action="/json/ajax/WinLog.aspx" method="post"enctype="multipart/form-data" > 
<div style="text-align: center"> 
填寫使用者資訊 
<hr style="size: 50%" /> 
</div> 
<div style="text-align: left"> 
使用者名稱:<input name="user_name" type="text" /><br /> 
密碼: 
<input name="Password1" type="password" /><br /> 
確認密碼:<input name="Password2" type="password" /><br /> 
性別:<input name="女" type="radio" value="女" />女<input id="Radio1" type="radio" value="男" 
name="sex" />男<br /> 
上傳照片:<input id="File1" type="file" name="picture" /><br /> 
<br /> 
<input type="submit" value="提交" /> 
<input type="reset" value="重置" /></div> 
</form> 
</body> 
</html>

  後臺:


namespace benz_CSI.json.ajax 

public partial class WinLog : System.Web.UI.Page 

protected void Page_Load(object sender, EventArgs e) 

Response.Write("利用 Response物件獲取客戶端資料"); 
Response.Write("<hr/>"); 
string[] names = Request.Form.AllKeys;//定義name儲存表單中的所有鍵即元素名稱 
for (int i = 0; i < names.Length; i++) 

string[] values = Request.Form.GetValues(i);//定義values儲存表單中所有鍵的鍵值 
for (int j = 0; j < values.Length; j++) 
Response.Write(names[i] + "=" + values[j] + "<br/>"); 

Response.Write("<hr/>"); 
Response.Write(Request.Form["picture"]); 
SaveFile(Request.Form["picture"]); 
}


public bool SaveFile(string filePath) 

if (Request.Files.Count >= 1) 

HttpPostedFile postedFile = Request.Files[0]; 
string fileName, fileExtension; 
fileName = System.IO.Path.GetFileName(postedFile.FileName); //檔名稱 
if (fileName != "") 

fileExtension = System.IO.Path.GetExtension(fileName); //上傳檔案的副檔名 
string new_filename = DateTime.Now.ToString("yyyyMMddHHmmss") + fileExtension; //給檔案重新命名 
string FileTemp = Page.MapPath("/json/ajax/upload/"); 
postedFile.SaveAs(FileTemp + new_filename); 
Response.Write("檔案 類 型:" + postedFile.ContentType.ToString() + "<br/>"); 
Response.Write("檔案新名稱:" + new_filename + "<br/>"); 
Response.Write("檔案原名稱:" + fileName + "<br/>"); 
Response.Write("副檔名:" + fileExtension + "<br/>"); 
Response.Write("檔案原路徑:" + postedFile.FileName + "<br/>"); 
Response.Write("檔案新路徑:" + FileTemp + new_filename + "<br/>"); 

else 

Response.Write("false"); 

}

}

相關推薦

html form提交資料後臺獲取

http://www.tuicool.com/articles/m67vMbQ 前臺: HTML的程式碼:(關鍵是要在form裡設定enctype="multipart/form-data",這樣才能在提交表單時,將檔案以二進位制流的形式傳輸到伺服器) <ht

form提交檔案到後臺 判斷檔案大小和型別

前臺html程式碼 <!--第一步:enctype="multipart/form-data"--> <form id="addForm" enctype="multipart/

form提交資料的同時上傳檔案程式碼示例

                              form表單提交資料的同時在表單中上傳檔案程式碼示例    一

html form提交前驗證

可以使用form表單的onsubmit方法,在提交表單之前,對錶單或者網頁中的資料進行檢驗。 onsubmit指定的方法返回true,則提交資料;返回false不提交資料。   <HTML> <head> <meta htt

js處理得到的值怎麼跟隨form提交到servlet後臺

案例: 背景:通過js獲取select被選中的值,但是js處理得到的值ss是需要跟隨form表單提交到servlet後臺 我在jsp某個位置;但是這個input要在form表單裡面; display: block;可以看見的 display: none;隱藏起

layui中form提交資料的方式。

<form class="layui-form"> <div class="layui-form-item"> <input type="text" id="subTitle" name="subTitle" lay-verify="req

html form提交

前要:利用themleaf模板使用th:checked提交資料怎麼選擇都是on 奇怪的很,只好回去使用原來的form表單提交了重新測試了一遍發現只能必須只能寫name 而不是在div radio的範圍內部寫th:field="*{serviceAttitudeScore}",

Form提交資料的幾種方式

一、submit提交 在form標籤中新增Action(提交的地址)和method(post),且有一個submit按鈕(<input type=’submit’>)就可以進行資料的提交,每一個input標籤都需要有一個name屬性,才能進行提交。 <form action=’http:/

HTML5第8次課堂筆記( 模擬form提交資料,xml的解析,jQuery的Ajax方法使用, mui的ajax)

HTML5第8次課堂筆記 1.  模擬form表單提交資料:(get方式)   <body>   <formmethod="get"action="DataTest7">   <inputtype="text"name="uname"valu

springMVC資料傳遞案例------利用form提交資料

這一章簡單的例舉一下form表單是怎麼講資料傳遞到後臺,又是怎麼接收,再傳回來 執行用的框架是之前搭建的,已經匯出來了,有興趣的可以直接去下載 ssm框架 在這個框架的基礎上加入了幾個jsp頁

HTML form 提交方式get和post的區別

method屬性規定如何傳送表單的資料。有兩種提交的方法分別為get和post。 1、get:提交的資料量要小於1024位元組,表單提交時表單域數值(表單請求的資訊:賬號、密碼…)將在位址列顯示。

form提交資料 同時提交圖片 jsp+contrller

jsp 頁面 <form id= "form1" action="${pageContext.request.contextPath}/information/addInformation.shtml" method="post" onsubmit="c

html form提交action和url跳轉到action有什麼區別.

form表單的action 與url跳轉是不一樣的: form表單可以給後臺傳遞資料,在後臺直接可以用request物件去向前臺請求資料。 form表單傳遞資料有兩種方式: method="post":這是傳遞大量資料時用的,在資料傳遞之前會先將資料打包,因此這種傳遞資料

Ajax獲取form提交資料

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta

提交資料以及後臺儲存小結

一、表單頁面分析 1,提交的引數:表單的內容 //ajax的post方式提交表單 //$("form").serialize()將表單序列號為key-value的形式的字串 //(這裡注意,在form之中的input裡需要增加 name) $.post("/item/s

html form提交後不重新整理不跳轉的實現方法------

如何實現表單提交後不重新重新整理當前頁面<form name='form1' id='form1' action='/xbcw/cw/xx_xx.action' method='post'>   <input type="button" id="btnSubmit" name="btnSub

Form提交資料的兩種方式

    在JavaWeb專案中,表單提交資料的方式時常可見,方式有兩種,其一,get方式;其二,post方式,下面簡單的來說說這兩種方式的區別。     1、從安全形度來說,當表單資料以get方式提交

JFinal form提交請求引數的獲取問題

使用JFinal時候遇到兩個問題,很隱蔽,折騰了半天,拿出來分享,希望能讓大家不再繼續糾結。 現象:無法獲取前端表單提交的請求引數,後臺使用getPara("paramName")輸出為null. 原因:網上查閱得知,此問題僅在Tomcat下出現,主要是因為如果actio

HTML提交資料 application/x-www-form-urlencoded 和 multipart/form-data 區別

概述: 在 Form 元素的語法中,EncType 表明提交資料的格式。 用 Enctype 屬性指定將資料回發到伺服器時瀏覽器使用的編碼型別。  說明:  application/x-www-form-urlencoded:窗體資料被編碼為名稱/值對。這是標準的編

HTML提交資料中application/x-www-form-urlencoded和multipart/form-data的區別

在Form元素的語法中,EncType表明提交資料的格式 用 Enctype 屬性指定將資料回發到伺服器時瀏覽器使用的編碼型別。 下邊是說明: application/x-www-form-urlencoded : 窗體資料被編碼為名稱/值對。這是標準的編