在我的应用程序中,我想从客户端上传一些图像。我想在没有任何处理程序的情况下这样做。我想使用 jquery 从客户端(本地驱动器)浏览图像,并希望将该图像传递给一个 Web 服务(从 jquery 本身)并仅使用该服务保存该图像,而不使用任何其他处理程序。
我尝试了与此类似的不同帖子,但没有一个能满足我的需求。
我是 .net 和 jquery 的新手,所以无论是否可能,我都尝试获取上传图像的路径,但后来我了解到由于安全浏览器不会从客户端提供实际路径。
那么有没有其他办法可以做到这一点.........
最佳答案
试试这段代码。愿这段代码能帮到你 为您的 Web 服务创建 ajax POST 请求。
<script type="text/javascript">
$(document).ready(function () {
$('#btnUploadFile').on('click', function () {
var data = new FormData();
var files = $("#fileUpload").get(0).files;
// Add the uploaded image content to the form data collection
if (files.length > 0) {
data.append("UploadedImage", files[0]);
}
// Make Ajax request with the contentType = false, and procesDate = false
var ajaxRequest = $.ajax({
type: "POST",
url: "/api/fileupload/uploadfile",
contentType: false,
processData: false,
data: data
});
ajaxRequest.done(function (xhr, textStatus) {
// Do other operation
});
});
});
</script>
您将在网络请求中获取文件内容
// Get the uploaded image from the Files collection
var httpPostedFile = HttpContext.Current.Request.Files["UploadedImage"];
http://www.codeproject.com/Articles/806075/File-Upload-using-jQuery-AJAX-in-ASP-NET-Web-API
关于javascript - 在 ASP 中仅使用 jquery 和 Web 服务从客户端上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39894812/