简体   繁体   English

在ASP.NET MVC中使用Java脚本将上传的图像设置为背景

[英]Setting Uploaded Image as Background using Javascript in ASP.NET MVC

In my application, I have so far managed to upload images to the server. 到目前为止,在我的应用程序中,我设法将图像上传到服务器。 Now I would like to set an uploaded image as a background dynamically using javascript in ASP.NET MVC5. 现在,我想使用ASP.NET MVC5中的JavaScript将上传的图片动态设置为背景。 Here is my code. 这是我的代码。 This part reads file from local storage 此部分从本地存储读取文件

jQuery(document).ready(function () 
{
    jQuery("#imageBrowes").change(function () 
    {
        var File = this.files;
        if (File && File[0]) 
        {
            ReadImage(File[0]);
        }

    })

})


var ReadImage = function(file) 
{
     var reader = new FileReader;
     var image = new Image;

     reader.readAsDataURL(file);
     reader.onload = function(_file) 
     {
         image.src = _file.target.result;
         image.onload = function() 
         {
             var height = this.height;
             var width = this.width;
             var type = file.type;
             var size = ~~(file.size / 1024) + "KB";

             jQuery("#targetImg").attr('src', _file.target.result);
             jQuery("#description").text("Size:" + size + ", " + height + "X " + width + ", " + type + "");
             jQuery("#imgPreview").show();

         }

     }

}   

This part stores the image into the file server system: 这部分将映像存储到文件服务器系统中:

var ClearPreview = function(){

jQuery("#imageBrowes").val('');
jQuery("#description").text('');
jQuery("#imgPreview").hide();

}

var Uploadimage = function()
{

var file = jQuery("#imageBrowes").get(0).files;
var data = new FormData;
data.append("ImageFile", file[0]);

jQuery.ajax({

type: "Post",
url: "/Home/ImageUpload",
data: data,
contentType: false,
processData: false,
success: function (response) {
ClearPreview();
jQuery("#uploadedImage").append('<img src="/elements-images/' + response + '" class="img-responsive thumbnail"/>');

}

})

}    

This the action that is called up in the above javascript: 这是在上面的javascript中调用的操作:

public ActionResult ImageUpload(ProductViewModel model)
{
    var file = model.ImageFile;

    if (file != null)
    {
        var fileName = Path.GetFileName(file.FileName);
        var extention = Path.GetExtension(file.FileName);
        var filenamewithoutextension = Path.GetFileNameWithoutExtension(file.FileName);

        file.SaveAs(Server.MapPath("/elements-images/" + file.FileName));


    }

    return Json(file.FileName, JsonRequestBehavior.AllowGet);

}

So basically the image file is saved into elements-images folder. 因此,基本上,图像文件保存在elements-images文件夹中。 How do I then set a specific image from that folder into a background image? 然后,如何将该文件夹中的特定图像设置为背景图像?

Javascript side preview JavaScript侧面预览

For javascript side preview of the file, Please change your this line 对于文件的javascript侧预览,请更改此行

jQuery("#targetImg").attr('src', _file.target.result);

to

jQuery("#targetImg").attr('src', "data:" + file.type + ";base64," +  btoa(_file.target.result) );

Preview after upload on server 在服务器上载后预览

To show image after server response your code seems to be correct. 要在服务器响应后显示图像,您的代码似乎是正确的。 It seems you need to change this line in action result 看来您需要在行动结果中更改此行

return Json(file.FileName, JsonRequestBehavior.AllowGet);

so it looks like this 所以看起来像这样

public ActionResult ImageUpload(ProductViewModel model)
{
    var file = model.ImageFile;
    string fileName = string.Empty;
    if (file != null)
    {
        fileName = Path.GetFileName(file.FileName);
        var extention = Path.GetExtension(file.FileName);
        var filenamewithoutextension = Path.GetFileNameWithoutExtension(file.FileName);

        file.SaveAs(Server.MapPath("/elements-images/" + file.FileName));


    }

    return Json(fileName, JsonRequestBehavior.AllowGet);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM