表单 布局:
<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();
}