html:
<input type="file" name="files" id="btnfile"/>
JS:
$('input[type=file]').on("change", function () {
var files = this.files[0];
var FileName = files.name;
var FileSize = files.size;
var reader = new FileReader();
var files = this.files[0];
var FileName = files.name;
var FileSize = files.size;
var reader = new FileReader();
reader.onload = function (e) {
$.ajax({
url: "Audit.aspx/SaveFileBinary",
type: "post",
dataType: "json",
contentType: "application/json",
async: false,
data: "{\"data\":{\"AuditID\":\"" + params["id"] + "\",\"FileData\":\"" + reader.result + "\",\"FileName\":\"" + FileName + "\",\"FileSize\":\"" + FileSize + "\"}}",
success: function (result) {
var res = $.parseJSON(result.d);
setDataSource(res); //刷新数据源
},
error: function (xhr, msg, e) {
alert(jQuery.parseJSON(xhr.responseText).Message);
}
})
}
reader.readAsDataURL(this.files[0])
});
$.ajax({
url: "Audit.aspx/SaveFileBinary",
type: "post",
dataType: "json",
contentType: "application/json",
async: false,
data: "{\"data\":{\"AuditID\":\"" + params["id"] + "\",\"FileData\":\"" + reader.result + "\",\"FileName\":\"" + FileName + "\",\"FileSize\":\"" + FileSize + "\"}}",
success: function (result) {
var res = $.parseJSON(result.d);
setDataSource(res); //刷新数据源
},
error: function (xhr, msg, e) {
alert(jQuery.parseJSON(xhr.responseText).Message);
}
})
}
reader.readAsDataURL(this.files[0])
});
后台:
string FileData= data["FileData"].ToString();
string strBase64 = FileData.Substring(FileData.IndexOf(",") + 1);
byte[] byteArray = Convert.FromBase64String(strBase64);
string strBase64 = FileData.Substring(FileData.IndexOf(",") + 1);
byte[] byteArray = Convert.FromBase64String(strBase64);
byteArray直接存入数据库就行了。
前台弄个列表,直接放个链接下载,很方便。