標籤:
在使用手機向webapi發送照片請求的時候遇到一個問題,關於base64位字串碼無法識別,提交的表單是這樣的:
<form id="form1" method="post" action="http://localhost:10322/food/Buy?date=‘2015-2-12‘&content=balabala內容"><div><input type="text" name="PhotoInfo" value="base位碼"/><input type="submit" value="提交" /></div></form>
由於PhotoInfo特別長,不能放在url拼接,會報異常,所以放在form裡面提交,但是api端卻捕捉不到,產生url異常.
解決方案:在對應的buy方法的PhotoInfo參數前面加上[Frombody]標記
public ResponseMessage Buy(string date,string content,[FromBody] string PhotoInfo)
加上這個標記後,webapi端能找到了,但是又有問題出現了,photoInfo明明有值,傳進來卻是null,查了一下,預設string類型不會傳值,必須用自訂類型,好吧,定義個自訂類型
public class Photo { public string Extension { get; set; } public string PhotoImage { get; set; } }
然後在參數使用的地方改成
public ResponseMessage Buy(string date,string content,[FromBody] Photo PhotoInfo)
取值的地時候要用PhotoInfo.PhotoImage,這時候就有值了,另外,在傳base64位碼的時候,前面的data:image/gif;base64, 這個不能加上,加上識別不了。
html複雜url提交