單獨作圖片上傳很簡單,若是要客戶端要上傳頭像保存到服務器就要稍微麻煩一點點了。數據庫
很少說了,直接上源碼:json
private void Upload() 服務器
{測試
string jsonInfo = string.Empty;orm
///這句是關鍵,它是獲取HTTP中文件流 的對象集合。對象
HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;圖片
string mobile = string.IsNullOrEmpty(Request.Form["mobile"]) ? Request.QueryString["mobile"] : Request.Form["mobile"];源碼
string fName = "";string
// string.IsNullOrEmpty(Request.Form["filename"]) ? Request.QueryString["filename"] : Request.Form["filename"];it
string status = string.Empty;
string error = string.Empty;
///在這裏咱們只取其中一個上傳對象操做
fName = hfc[0].FileName;
try
{
string[] list;
if (!string.IsNullOrEmpty(fName))
{
list = fName.Split('.');
if (list.Length > 1)
{
string ftype = list[1].ToLower();
if (ftype == "jpg" || ftype == "png" || ftype == "jpeg")
{
fName = mobile + "." + list[1];
string filePath = "../CSS/headmiages/";
hfc[0].SaveAs(System.IO.Path.Combine(MapPath(filePath), fName));
RockUserInfo userInfo = new RockUserInfo();
userInfo.LogOnPhoneNum = mobile;
//根據手機號查詢出用戶基本信息
userInfo = SearchByPhone(userInfo);
userInfo.filename = fName;
//插入頭像信息保存
Save(userInfo);
status = "0";
error = "";
}
else
{
status = "1";
error = "請上傳 .jpg/.png/.jpeg/類型的圖片";
}
}
}
else
{
status = "1";
error = "請上傳 .jpg/.png/.jpeg/類型的圖片";
}
//fileload.in
}
catch (Exception ex)
{
status = "2";
error = ex.ToString();
}
jsonInfo = "{\"" + "status" + "\":\"" + status + "\"," + "msg" + "\":\"" + error + "}";
Response.Write(jsonInfo);
}
這樣就 OK了,在服務器端保存上傳文件,操做數據庫就基本達到目標了。很簡單吧!
這段代碼通過測試是沒有問題的,若是有不合理之處還望你們指出來共同進步。