表單 佈局:
<form method="post" enctype="multipart/form-data" id="form_UpFile" name="form_UpFile">
<input type="file" accept="image/*" capture="camera" id="file_upfiles" class="external" style="width:170px;" />
<button type="button" id="btn_upload" class="external">上傳</button>
<input type="text" value="" id="picPath" style="display:none;" />
</form>
//上傳圖片或者文件方法
function UploadOther() {
$(“#btn_upload”).on(“click”, function (e) {
alert(“s”);
var fd = new FormData();
fd.append("file_upfiles", $("#file_upfiles").get(0).files[0]);
fd.append("gid", userId);
console.info(fd);
$.ajax({
url: "ajaxpage/GetHelper.ashx",
type: "POST",
data: fd,
cache: false,
processData: false,
contentType: false,
success: function (d) {
var josn = eval("(" + d + ")");
console.info(josn);
console.info(josn.filename);
$$("#form1 #picPath").val(josn.fileid);
$$("#form1 #hs_pic").val(josn.fileid);
path = josn.repath;
console.info(d);
}
});
});
}
後臺方法:
///
/// 上傳文件
///
///
private string fileUpLoad(HttpContext context)
{var userid=context.Request[“gid”];
string uploadPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + “Upload/”;
string result = string.Empty;
string imgPath = string.Empty;
string temp_Guid = Guid.NewGuid().ToString();
string province = “”;
string fName = “”;
StringBuilder gtreturn = new StringBuilder();
StringBuilder sql = new StringBuilder();
float MyFileSize= 0;
HttpRequest request = System.Web.HttpContext.Current.Request;
HttpFileCollection fileCollection = request.Files;
string img_type = “”;
// 判斷是否有文件
if (fileCollection.Count > 0)
{
// 獲取文件
HttpPostedFile httpPostedFile = fileCollection[0];
// 文件擴展名
string fileExtension = System.IO.Path.GetExtension(httpPostedFile.FileName);
var g_type=fileExtension.Split('.');
img_type = "image/" + g_type[1]; //文件類型
string fileName = System.IO.Path.GetFileName(httpPostedFile.FileName);
fName = fileName;
MyFileSize = (float)httpPostedFile.ContentLength ;//獲取文件的大小
/// (1024 * 1024)
// 文件名稱
fileName = Guid.NewGuid().ToString() + "_" + fileName;
// 文件上傳路徑
string filePath = uploadPath + fileName;
// 驗證文件格式
// 如果目錄不存在則要先創建
if (!System.IO.Directory.Exists(uploadPath))
{
System.IO.Directory.CreateDirectory(uploadPath);
}
// 保存新的文件
while (System.IO.File.Exists(filePath))
{
fileName = temp_Guid + fileExtension;
filePath = uploadPath + fileName;
}
httpPostedFile.SaveAs(filePath);
result = filePath;
sql.AppendFormat(" INSERT INTO library_file VALUES ( ");
sql.AppendFormat(" 'hailing','{0}','','{1}','{2}','{3}',",temp_Guid,temp_Guid,fileName,img_type);
sql.AppendFormat(" '{0}','{1}','{2}','{3}','{4}','{5}',", MyFileSize, fileExtension,"","image",null,"ACTIVE");
sql.AppendFormat(" '{0}','{1}','{2}','{3}','{4}','{5}','{6}',", DateTime.Now.ToString(), null,null,null,null,null,null);
sql.AppendFormat(" '{0}','{1}','{2}','{3}','{4}'", userid, null, null, null, null);
sql.AppendFormat(" )");
gtreturn.Append("{");
OperationDB odb = new OperationDB();
odb.setTableData(sql.ToString());
gtreturn.Append("\"filename\":\"" + fileName + "\",");
gtreturn.Append("\"fileid\":\"" + temp_Guid + "\",");
gtreturn.Append("\"repath\":\"../Upload/" + fileName + "\"");
}
else
{
result = "請先選擇文件!";
gtreturn.Append("\"absoultPath\":\"" + result + "\",");
}
gtreturn.Append("}");
return gtreturn.ToString();
}