我已经编写了用于上传个人资料图片的代码,但在上传文件后,它会在我上传的文件夹中排除格式。怎么可能包含这个或者你还有其他任何上传文件的解决方案吗?
$("#btnUploadFile").click(function (evt) {
alert('dfdfd');
var imgup = 'img';
var files = $("#fileUpload").get(0).files;
if (files.length > 0) {
var data = new FormData();
for (i = 0; i < files.length; i++) {
data.append("file" + i, files[i]);
}
$.ajax({
type: "POST",
url: '/api/EmployeeMasterApi/UploadFile/'+'?imgup' + imgup,
contentType: false,
processData: false,
data: data,
// data: JSON.stringify(data),
success: function (messages) {
for (i = 0; i < messages.length; i++) {
alert('dddd');
alert(messages[i]);
}
},
error: function () {
alert("Error while invoking the Web API");
}
});
}
});
我在c#中使用以下代码进行文件上传。
[HttpPost]
public void UploadFile([FromUri] string[] imgup)
{
if (HttpContext.Current.Request.Files.AllKeys.Any())
{
// Get the uploaded image from the Files collection
var httpPostedFile = HttpContext.Current.Request.Files["file0"];
if (httpPostedFile != null)
{
// Validate the uploaded image(optional)
// Get the complete file path
var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/imgUpload"), httpPostedFile.FileName);
// Save the uploaded file to "UploadedFiles" folder
httpPostedFile.SaveAs(fileSavePath);
}
}
}
这里工作正常。将“imgUpload”文件夹中的图像上传后,将其排除在Format.please中,以帮助解决此问题