是我這個put形式傳遞的參數(shù)有問題嗎?put形式傳遞文件不能這樣嗎?望大神能為我解答一二。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input id="file" name="img" type="file"/>
</body>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>
$('#file').on('change',function(){
var formData = new FormData();
formData.append("img", $("#file")[0].files[0]);
console.log(formData,'form', $("#file")[0].files[0])
$.ajax({
url: "xxx",
type: "put",
data: formData,
processData: false,
contentType: false,
xhrFields:{withCredentials: true},
success: function(response){
console.log(response)
// 根據(jù)返回結(jié)果指定界面操作
}
});
})
</script>
</html>
首先,先解析一下 POST
與 PUT
的區(qū)別
HTTP/1.1協(xié)議中共定義了8中請求方法:GET、HEAD、POST、PUT、DELETE、CONNECT、OPTIONS、TRACE,其中用的相對較多的有下面四種:
GET 獲取資源
PUT 更新或創(chuàng)建資源
POST 創(chuàng)建資源
DELETE 刪除資源
RESTful api則充分利用了HTTP協(xié)議,每個資源對應(yīng)一個具體的URI,然后利用不同的HTTP操作對應(yīng)增刪改查,如:
POST /uri 創(chuàng)建
DELETE /uri/xxx 刪除
PUT /uri/xxx 更新或創(chuàng)建
GET /uri/xxx 查看
可以看到,GET和DELETE對應(yīng)的操作非常明確,但是POST與PUT都可以進(jìn)行資源的創(chuàng)建,那么什么時候用POST什么時候用PUT呢
這就需要了解HTTP協(xié)議中的另一個重要性質(zhì):冪等
什么是冪等
要理解PUT和POST的區(qū)別,還要知道HTTP協(xié)議中的一個重要性質(zhì),冪等(Idempotence):
Methods can also have the property of “idempotence” in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request.
什么個意思呢?HTTP協(xié)議中的冪等指的是一個資源無論請求多少次,對他產(chǎn)生的副作用是一樣的
GET操作是安全的,也就是不管操作多少次,資源的狀態(tài)都不會改變,所以GET也是冪等的
PUT和DELETE是冪等的,比如我用PUT或者DELETE修改一個資源,不管操作多少次,每次操作后的結(jié)果并沒有什么不同
POST操作既不是安全的,也不是冪等的,如果常見的POST重復(fù)加載的問題,我們進(jìn)行了多少次POST的操作,最后就創(chuàng)建了多少個資源,這也是為什么Chrom等瀏覽器,在刷新POST請求時會有彈窗提示
所以,你使用PUT上傳,是沒有問題,使用POST上傳也是可以。
但是,你使用PUT上傳的時候,需要后端處理 數(shù)據(jù)
,把相應(yīng)的 header 頭數(shù)據(jù),名字、大小等獲取到,再保存寫入文件就可以。
前端代碼,這樣處理數(shù)據(jù)沒有問題,后端處理如下(以PHP為例子)
public function getPut(string! name = null, var filters = null, var defaultValue = null, boolean notAllowEmpty = false, boolean noRecursive = false) -> var
{
var put;
let put = this->_putCache;
if typeof put != "array" {
let put = [];
parse_str(this->getRawBody(), put);
let this->_putCache = put;
}
return this->getHelper(put, name, filters, defaultValue, notAllowEmpty, noRecursive);
}
public function getRawBody() -> string
{
var rawBody, contents;
let rawBody = this->_rawBody;
if empty rawBody {
let contents = file_get_contents("php://input");
/**
* We need store the read raw body because it can't be read again
*/
let this->_rawBody = contents;
return contents;
}
return rawBody;
}
這個就是put的處理方式 contents = file_get_contents("php://input");
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。