表單提交文件上傳
『壹』 ajax怎樣提交form表單與實現文件上傳
Ajax 提交form方式可以將form表單序列化 然後將數據通過data提交至後台,例如:
『貳』 如何使用multipart/form-data格式上傳文件
在網路編程過程中需要向伺服器上傳文件。Multipart/form-data是上傳文件的一種方式。
Multipart/form-data其實就是瀏覽器用表單上傳文件的方式。最常見的情境是:在寫郵件時,向郵件後添加附件,附件通常使用表單添加,也就是用multipart/form-data格式上傳到伺服器。
表單形式上傳附件
具體的步驟是怎樣的呢?
首先,客戶端和伺服器建立連接(TCP協議)。
第二,客戶端可以向伺服器端發送數據。因為上傳文件實質上也是向伺服器端發送請求。
第三,客戶端按照符合「multipart/form-data」的格式向伺服器端發送數據。
既然Multipart/form-data格式就是瀏覽器用表單提交數據的格式,我們就來看看文件經過瀏覽器編碼後是什麼樣子。
這行指出這個請求是「multipart/form-data」格式的,且「boundary」是 「---------------------------7db15a14291cce」這個字元串。
不難想像,「boundary」是用來隔開表單中不同部分數據的。例子中的表單就有 2 部分數據,用「boundary」隔開。「boundary」一般由系統隨機產生,但也可以簡單的用「-------------」來代替。
實際上,每部分數據的開頭都是由"--" + boundary開始,而不是由 boundary 開始。仔細看才能發現下面的開頭這段字元串實際上要比 boundary 多了個 「--」
緊接著 boundary 的是該部分數據的描述。
接下來才是數據。
「GIF」gif格式圖片的文件頭,可見,unknow1.gif確實是gif格式圖片。
在請求的最後,則是 "--" + boundary + "--" 表明表單的結束。
需要注意的是,在html協議中,用 「 」 換行,而不是 「 」。
下面的代碼片斷演示如何構造multipart/form-data格式數據,並上傳圖片到伺服器。
//---------------------------------------
// this is the demo code of using multipart/form-data to upload text and photos.
// -use WinInet APIs.
//
//
// connection handlers.
//
HRESULT hr;
HINTERNET m_hOpen;
HINTERNET m_hConnect;
HINTERNET m_hRequest;
//
// make connection.
//
...
//
// form the content.
//
std::wstring strBoundary = std::wstring(L"------------------");
std::wstring wstrHeader(L"Content-Type: multipart/form-data, boundary=");
wstrHeader += strBoundary;
HttpAddRequestHeaders(m_hRequest, wstrHeader.c_str(), DWORD(wstrHeader.size()), HTTP_ADDREQ_FLAG_ADD);
//
// "std::wstring strPhotoPath" is the name of photo to upload.
//
//
// uploaded photo form-part begin.
//
std::wstring strMultipartFirst(L"--");
strMultipartFirst += strBoundary;
strMultipartFirst += L" Content-Disposition: form-data; name="pic"; filename=";
strMultipartFirst += L""" + strPhotoPath + L""";
strMultipartFirst += L" Content-Type: image/jpeg ";
//
// "std::wstring strTextContent" is the text to uploaded.
//
//
// uploaded text form-part begin.
//
std::wstring strMultipartInter(L" --");
strMultipartInter += strBoundary;
strMultipartInter += L" Content-Disposition: form-data; name="status" ";
std::wstring wstrPostDataUrlEncode(CEncodeTool::Encode_Url(strTextContent));
// add text content to send.
strMultipartInter += wstrPostDataUrlEncode;
std::wstring strMultipartEnd(L" --");
strMultipartEnd += strBoundary;
strMultipartEnd += L"-- ";
//
// open photo file.
//
// ws2s(std::wstring)
// -transform "strPhotopath" from unicode to ansi.
std::ifstream *pstdofsPicInput = new std::ifstream;
pstdofsPicInput->open((ws2s(strPhotoPath)).c_str(), std::ios::binary|std::ios::in);
pstdofsPicInput->seekg(0, std::ios::end);
int nFileSize = pstdofsPicInput->tellg();
if(nPicFileLen == 0)
{
return E_ACCESSDENIED;
}
char *pchPicFileBuf = NULL;
try
{
pchPicFileBuf = new char[nPicFileLen];
}
catch(std::bad_alloc)
{
hr = E_FAIL;
}
if(FAILED(hr))
{
return hr;
}
pstdofsPicInput->seekg(0, std::ios::beg);
pstdofsPicInput->read(pchPicFileBuf, nPicFileLen);
if(pstdofsPicInput->bad())
{
pstdofsPicInput->close();
hr = E_FAIL;
}
delete pstdofsPicInput;
if(FAILED(hr))
{
return hr;
}
// Calculate the length of data to send.
std::string straMultipartFirst = CEncodeTool::ws2s(strMultipartFirst);
std::string straMultipartInter = CEncodeTool::ws2s(strMultipartInter);
std::string straMultipartEnd = CEncodeTool::ws2s(strMultipartEnd);
int cSendBufLen = straMultipartFirst.size() + nPicFileLen + straMultipartInter.size() + straMultipartEnd.size();
// Allocate the buffer to temporary store the data to send.
PCHAR pchSendBuf = new CHAR[cSendBufLen];
memcpy(pchSendBuf, straMultipartFirst.c_str(), straMultipartFirst.size());
memcpy(pchSendBuf + straMultipartFirst.size(), (const char *)pchPicFileBuf, nPicFileLen);
memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen, straMultipartInter.c_str(), straMultipartInter.size());
memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen + straMultipartInter.size(), straMultipartEnd.c_str(), straMultipartEnd.size());
//
// send the request data.
//
HttpSendRequest(m_hRequest, NULL, 0, (LPVOID)pchSendBuf, cSendBufLen)
『叄』 SpringMVC表單提交時,多文件上傳和單個文件上傳有些什麼區別
基於Spring3 MVC實現基於form表單文件上傳
一:雜項准備
環境搭建參考這里
二:前台頁面
根據RFC1867,只要在提交form表單中聲明提交方法為POST,enctype屬
性聲明為multipart/form-data, action聲明到要提交的url即可。具體如下:
三:spring配置
使用spring3的MultipartHttpReqest來接受來自瀏覽器的發送的文件內容。
需要配Multipart解析器在express-servlet.xml中。內容如下:
同時還需要在maven的pom.xml文件添加apachefileupload與common-io兩個包。
四:Controller中方法實現
[java] view plain
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ModelAndView getUploadFile(HttpServletRequest request, HttpServletResponse response) {
System.out.println("fucking spring3 MVC upload file with Multipart form");
String myappPath = request.getSession().getServletContext().getRealPath("/");
try {
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
System.out.println("fucking spring3 MVC upload file with Multipart form");
// String myappPath = multipartRequest.getServletContext().getRealPath("/");
// does not work, oh my god!!
MultipartFile file = multipartRequest.getFiles("userfile1").get(0);
long size = file.getSize();
byte[] data = new byte[(int) size];
InputStream input = file.getInputStream();
input.read(data);
// create file, if no app context path, will throws access denied.
// seems like you could not create any file at tomcat/bin directory!!!
File outFile = new File(myappPath + File.separator + file.getOriginalFilename());
if(!outFile.exists()) {
outFile.createNewFile();
System.out.println("full path = " + outFile.getAbsolutePath());
} else {
System.out.println("full path = " + outFile.getAbsolutePath());
}
FileOutputStream outStream = new FileOutputStream(outFile);
outStream.write(data);
outStream.close();
input.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return new ModelAndView("welcome");
}
『肆』 java表單提交裡面的文件上傳,用同步的方式好還是用非同步的好啊
你好,很高興回答你的問題。
這里做成非同步比較好。如果同步的話,遇到文件大的話,表單提交會很慢,體驗非常不好。
如果有幫助到你,請點擊採納。
『伍』 ajax怎麼提交帶文件上傳表單
上傳的文件是沒有辦法和表單內容一起非同步的,可考慮使用jquery的ajaxfileupload,或是其他的插件,非同步上傳文件後,然後再對表單進行操作。
『陸』 怎麼在上傳文件的同時提交表單
可以用「風聲無組件」上傳,如果還想獲取除了上傳文件以外的其他提交信息,只要在上傳類後面讀取就可以了:
以下為檢驗頁面代碼:
<!--#include file="FSUpClass.asp"-->
'--上傳類函數開始--
dim upload
set upload=New UpLoadClass
upload.MaxSize = 1048000
upload.FileType = "jpg/gif/png/bmp"
'上傳文件存放目錄
upload.SavePath = "Upfile/"
upload.open()
if upload.Error>0 then
response.write"<SCRIPT language=JavaScript>alert('上傳圖片只允許gif/jpg/png/bmp格式,且不能超過1MB。');"
response.write"javascript:history.go(-1)</SCRIPT>"
end if
'--上傳類函數結束--
set rs=server.createobject("adodb.recordset")
sql="select * from Table where...."
rs.open sql,conn,1,3
rs.addnew
'Pic為你上傳的圖片的提交名
rs("Pic")=upload.form("Pic")
'text為你提交的文本信息
rs("text")=upload.form("text")
rs....
rs.update
rs.close
『柒』 關於AsyncHttpClient框架的post 提交表單上傳文件怎麼弄
用開源項目Asynchttpclient的GET_POST訪問網路
* 使用GET方式提交數據:
//1、創建一個瀏覽器對象
AsyncHttpClient client = new AsyncHttpClient();
//2、發送一個GET請求
client.get(path, new AsyncHttpResponseHandler() {
/**
* 請求處理成功後調用這個方法
* statusCode 響應碼 200 404 503
* headers 響應頭信息
* responseBody 伺服器返回的響應數據(如:登陸成功、失敗等)
*/
@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
Toast.makeText(MainActivity.this,
new String(responseBody), 0).show();
}
『捌』 asp 提交表單和上傳文件
asp.net和asp上傳方式基本相似。都需要使用到form表單。下面分別介紹asp和asp.net兩種文件上傳方式。
第一種:asp方式
首先建立form表單
<form name="form1" method="post" action="send.asp"enctype="multipart/form-data">
<input name="title" type="text"/>
<input name="uploadimg" type="file" />
<input name="submit" type="submit" value="提交"/>
</form>
asp的表單一定要注意加上enctype="multipart/form-data"這個屬性,否則是上傳不了圖片的,這個屬性很關鍵。
上傳處理代碼也就是send.asp的處理代碼。
因為asp本身沒有上傳的組件或控制項,這里只能藉助第三方式的組件或類。上傳組件推薦使用aspJpeg組件,這個組件不僅可以上傳文件,如果是圖片的話,可以調節尺寸尺寸,創建縮略圖等。很方便,目前一般的空間商都支持這個組件,另外就是使用組件上傳類,像風聲,無懼等,都是無組件上傳。
上傳代碼,這里假設採用的是風聲無組件上傳類。類文件已經包含進去。
<include file="uploadclass.asp"-->
處理代碼:
先初始化上傳類
dim myrequest
set myrequest = new UpLoadClass
設置屬性:
myrequest.FileType="gif,jpeg,jpg,png" //設置上傳類型
myrequest.SavePath="../upload/" //設置上傳路徑
myrequest.MaxSize=100*1024 //設置上傳文件的大小,
myrequest.AutoSave=1 //設置保存方式,為1表示自動保存
myrequest.Open //打開文件流
title =myrequest.form("title")
img =myrequest.form("uploadimg") //這兩部表示接收form表單中的數據。不 能再用request.form或request.querystring來接收了,必須用剛才初始化的對象
myrequest來接收。
現在數據接收到了,剩下的就是保存到資料庫。這和常用的數據保存方式一下的,拼接SQL.然後再執行,這里不贅述。
第二種asp.net方式。
asp.net方式,因為採用的是伺服器控制項,所以與asp有點區別。
首先是form不同,asp.net的form是伺服器控制項,需要添加ID和runat="Server"如
<form name="form1" ID="form1" runat="Server">
<asp:FileUpload ID="FileUpload1"runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上傳" /></form>
處理代碼,上傳並保存代碼
protectedvoidButton1_Click(objectsender, EventArgs e)
{
StringsavePath = Server.MapPath("upload");
if(FileUpload1.HasFile)
{
Stringfilename;
filename = FileUpload1.FileName;
savePath +=filename;
FileUpload1.SaveAs(savePath);
Page.Response.Write(FileUpload1.PostedFile.ContentType + FileUpload1.PostedFile.ContentLength+"<br>");
Page.Response.Write("<img src='"+savePath+"'>");}
else
{
Page.Response.Write("fff");
}
}
asp提交和上傳文件,與asp.net是不同的。一個是html標簽,一個是伺服器控制項。相對來說,asp.net的文件上傳比asp簡單很多,因為asp.net提供了相對應的上傳控制項。而asp沒有。只有採用第三方組件或無組件上傳類。上傳文件成功後,另外還可以加入更多的處理元素,如圖片入庫,加上水印等,這需要你自己去思考和查找資料了。
『玖』 如何利用curl實現form表單提交 帶文件上傳
php">//上傳D盤下的test.jpg文件,文件必須存在,否則curl處理失敗且沒有任何提示
$data=array('name'=>'Foo','file'=>'@d:/test.jpg');
註:PHP5.5.0起,文件上傳建議使用CURLFile代替@
$ch=curl_init('http://localhost/upload.php');
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_exec($ch);
更多內容請參考:http://www.zjmainstay.cn/php-curl#十模擬上傳文件
『拾』 怎麼將文件上傳和表單提交分開處理
上傳這樣的功能本來就應該獨立出來,為以後能夠更好的擴展和組件重用做准備。至於你的這個問題也很容易處理,問題的關鍵不在於後台處理的程序如何安排,而在於頁面的表現形式,因為程序本身既然獨立出來了,你的ACTION也是獨立出來的,那麼就把頁面里的上傳功能獨立出來。
舉例來說:
在你的表單里添加一個上傳按鈕,點擊後彈出一個漂浮的div圖層,然後在這個圖層里可以放置單個的文件瀏覽按鈕,或者動態的增加多個需要上傳的瀏覽按鈕,都是很靈活的,然後把這個圖層使用單獨的form標簽包圍起來就可以了。
類似的功能像很多論壇程序都在使用,比較成熟的js組件也可以實現,比如ExtJs、jQuery等等,一般來說能夠提供web editor功能的js組件對於上傳都是類似的處理方式。