我正在尝试使用REST调用Jquery Ajax将文件(任何类型)的文件(任何类型)上传到SQL Server中列数据类型为Image的数据库。 这就是我的业务服务的样子 公开小数点
CreateDairyAttachmentInterface(BusinessEntities.DairyAttachmentInterfaceEntity dairyEntity)
{
using (var scope = new TransactionScope())
{
var dairy = new ATTACHMENT_HEADER
{
DIARY_HEADER_ID = dairyEntity.DIARY_HEADER_ID,
ATTACHMENT_TYPE = dairyEntity.ATTACHMENT_TYPE,
ATTACHMENT_TITLE = dairyEntity.ATTACHMENT_TITLE,
ATTACHMENT_DESC = dairyEntity.ATTACHMENT_DESC,
ATTACHMENT_CATEGORY = dairyEntity.ATTACHMENT_CATEGORY,
ATTACHMENT_SHORT_TEXT = dairyEntity.ATTACHMENT_SHORT_TEXT,
FILE_NAME = dairyEntity.FILE_NAME,
FILE_TYPE = dairyEntity.FILE_TYPE,
FILE_DATA = dairyEntity.FILE_DATA,
CREATION_DATE = dairyEntity.CREATION_DATE,
CREATED_BY = dairyEntity.CREATED_BY,
LAST_UPDATE_DATE = dairyEntity.LAST_UPDATE_DATE,
LAST_UPDATE_LOGIN = dairyEntity.LAST_UPDATE_LOGIN,
ERP_TRANSFER_CHECK = dairyEntity.ERP_TRANSFER_CHECK,
PORTAL_TRANSFER_CHECK = dairyEntity.PORTAL_TRANSFER_CHECK,
INTERFACE_TRANSFER_CHECK = dairyEntity.INTERFACE_TRANSFER_CHECK,
IS_TEMP = dairyEntity.IS_TEMP
};
_unitOfWork.DairyAttacmentInterfaceRepository.Insert(dairy);
_unitOfWork.Save();
scope.Complete();
return dairy.ATTACH_HEADER_ID;
}
}
在上面的代码中,我得到了milkEntity.FILE_DATA = Null。除此以外的所有其他值都很好。
这就是我发送文件的方式。
function uploadingFile()
{
var dt = new Date($.now());
var nowd = dt.getUTCFullYear() + "/" + dt.getMonth() + "/" + dt.getDate();
var nowt = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
var dnt = nowd + " " + nowt;
var file = $('#attachmentFile')[0].files[0],
fileName = file.name,
fileType = file.type
fileSize = file.size;
var Token = $("#tokenkey").val();
var filedata;// = "test";
var reader = new FileReader();
var data1;
reader.addEventListener("load", function () {
var arrayBuffer = this.result,
array = new Uint8Array(arrayBuffer),
//binaryString = String.fromCharCode.apply(null, array);
console.log(binaryString);
data1={"DIARY_HEADER_ID":$("#diarynumber").val(),"ATTACHMENT_TYPE":$("#headerID").val(),"ATTACHMENT_TITLE":$("#attachmentTitle" ).val(),"ATTACHMENT_DESC":$("#attachmentDesc").val(),"ATTACHMENT_CATEGORY":"","ATTACHMENT_SHORT_TEXT":"","FILE_NAME":fileName,"FILE_TYPE":fileType,"FILE_DATA":array,"CREATION_DATE":dnt,"CREATED_BY":"0","LAST_UPDATE_DATE":dnt,"LAST_UPDATE_LOGIN":"0","ERP_TRANSFER_CHECK":"N","PORTAL_TRANSFER_CHECK":"Y","INTERFACE_TRANSFER_CHECK":"Y","IS_TEMP":"N"};
console.log(JSON.stringify(data1));
var urldata1= "http://localhost:55959/v1/Diary_Attach/Create";
$.ajax({
url: urldata1,
type: 'POST',
data: JSON.stringify(data1),
cache: false,
contentType: "application/json;charset=utf-8",
processData: false,
crossDomain: true,
dataType: "json",
headers: {
"Token": Token
},
success: function (response) {
alert("Document uploaded successfully.");
},
error: function (jqXHR, status) {
// error handler
console.log(jqXHR);
alert('fail' + status);
}
});
}, false);
if (file) {
reader.readAsArrayBuffer(file);
}
}
在上面的代码中,我正在使用许多其他参数上传文件。我正在转换文件,因为我的控制器希望我将文件发送为BYTE数组。我正在转换它,它转换正常。但是,在进行后期调用时,FILE_DATA为空。在console.log中显示正确时,如下所示
{"DIARY_HEADER_ID":"6","ATTACHMENT_TYPE":"40","ATTACHMENT_TITLE":"","ATTACHMENT_DESC":"","ATTACHMENT_CATEGORY":"","ATTACHMENT_SHORT_TEXT":"","FILE_NAME":"ADF-2.pdf","FILE_TYPE":"application/pdf","FILE_DATA":{"0":37,"1":80,"2":68,"3":70,"4":45,"5":49,"6":46,"7":51,"8":13,"9":37,"10":226,"11":227,"12":207,"13":211,"14":13,"15":49,"16":49,"17":32,"18":48,"19":32,"20":111,"21":98,"22":106,"23":13,"24":60,"25":60,"26":13,"27":47,"28":84,"29":121,"30":112,"31":101,"32":32,"33":47,"34":65,"35":110,"36":110,"37":111,"38":116,"39":32,"40":47,"41":83,"42":117,"43":98,"44":116,"45":121,"46":112,"47":101,"48":32,"49":47,"50":76,"51":105,"52":110,"53":107,"54":32,"55":47,"56":82,"57":101,
FILE_DATA是在这样的业务实体中定义的
public byte[] FILE_DATA { get; set; }
为什么rest调用没有向控制器发送BYTE ARRAY的值,为什么它总是为null?对我来说这已经是整天了,我快要死线了。请帮忙。
谢谢
答案 0 :(得分:0)
它将作为null
出现,因为ajax调用中的Url
不正确。尝试更新您的Ajax调用:
var urldata1= "/v1/Diary_Attach/CreateDairyAttachmentInterface"; //Example: /v1/ControllerName/ActionName
$.ajax({
url: urldata1,
type: 'POST',
data: {dairyEntity: JSON.stringify(data1)}, //Parameter matching name
cache: false,
contentType: "application/json;charset=utf-8",
processData: false,
crossDomain: true,
dataType: "json",
headers: {
"Token": Token
},
success: function (response) {
alert("Document uploaded successfully.");
},
error: function (jqXHR, status) {
// error handler
console.log(jqXHR);
alert('fail' + status);
}
});