var profileImage = fileInputInByteArray;
$.ajax({
url: 'abc.com/',
type: 'POST',
dataType: 'json',
data: {
// Other data
ProfileImage: profileimage
// Other data
},
success: {
}
})
// Code in WebAPI
[HttpPost]
public HttpResponseMessage UpdateProfile([FromUri]UpdateProfileModel response) {
//...
return response;
}
public class UpdateProfileModel {
// ...
public byte[] ProfileImage {get ;set; }
// ...
}Run Code Online (Sandbox Code Playgroud)
<input type="file" id="inputFile" />Run Code Online (Sandbox Code Playgroud)
我正在使用ajax调用将输入类型=文件输入的byte []值发布到以apip []格式接收的web api.但是,我遇到了获取字节数组的困难.我希望我们可以通过File API获取字节数组.
注意:在通过ajax调用之前,我需要先将字节数组存储在变量中
我在jQuery中完成了这个,从输入文件标签中获取文件名,使用jQuery它可以很好地工作.
//jQuery('input[type=file]').on('change', prepareUpload);
document.getElementsByTagName('input[type=file]').addEventListener('change',prepareUpload1,true);
/*
//this works in jQuery
function prepareUpload(event)
{
var files = event.target.files;
var fileName = files [0].name
alert(fileName);
}
*/
/****Check it here ****/
// it does not work in javascript
function prepareUpload1(event)
{
var files = event.target.files;
var fileName = files [0].name
alert("fileName 2 : "+fileName);
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" />Run Code Online (Sandbox Code Playgroud)
但我发现Event.target在IE中不起作用,我试图将其改为java脚本addeventlistener,它没有用.
它抛出错误
Uncaught ReferenceError: input is not defined
Run Code Online (Sandbox Code Playgroud)
它在jQuery中工作,但它在JS中不起作用,由于IE问题,我需要将其更改为JS.
有人可以提供帮助