简体   繁体   English

如何使用ASP.NET MVC4在单击时使用jquery在不使用任何形式的情况下上载文本框中的图像和文本

[英]How to upload image and the text in a textbox without using any form using jquery on click using ASP.NET MVC4

View code 查看代码

    <input type="text" id="txtImageName"></input>
    <input type="text" id="txtImageDescription"></input>
    <input type="file" id="txtImageName"></input>
    <input type="button" id="btnUpload" Value="Upload">

Jquery Code jQuery代码

<script>
        $('#btnUpload').click(function () {       
                var file = $("#txtImageName").get(0).files;
                $.ajax({
                    type: 'post',
                    url: "@Url.Action("HomeAddNew", "Admin")",
                    data:
                    {
                        ImageName: $('#txtImageNameoption').val(),
                        ImageDescription:$('#txtImageDescription').val(),                   
                        Image: file
                    },
                    success: function (data) {
                        alert('hai');
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
        });
    </script>

Controller code 控制器代码

public ActionResult HomeAddNew(string ImageName, string ImageDescription, HttpPostedFileBase Image)
        {
            //in the above HttpPostedFilebase I'm getting the null value but, I don't want to use .fileupload method or formdata class in jquery. 
            return View();
        }

Explanation : Whenever I check the file in controller method it is showing as null . 说明 :每当我在控制器方法中检查文件时,它都显示为null Please send me the perfect solution for this. 请给我发送完美的解决方案。 It will help me a lot. 这对我有很大帮助。 My email id is ganesh@concai.co.uk 我的电子邮件ID为ganesh@concai.co.uk

You can't upload a file with ajax. 您无法使用ajax上传文件。 If your browser requirements starts from IE10 then you can use xhr . 如果您的浏览器要求从IE10开始,则可以使用xhr

Code and information about how you should do it HERE . 代码和相关信息,你应该怎么做这里

Also, there's a plugin called FileAPI , which you can find HERE 另外,还有一个名为FileAPI的插件,您可以在这里找到

Simple demo with progress bar 具有progress bar简单演示

function startUpload() {
var fileInput = document.getElementById("fileInput");

if (fileInput.files.length == 0) {
    alert("Please choose a file");
    return;
}

var progressBar = document.getElementById("progressBar");
var xhr = new XMLHttpRequest();

xhr.upload.onprogress = function(e) {
    var percentComplete = (e.loaded / e.total) * 100;
    progressBar.value = percentComplete;
};

xhr.onload = function() {
    if (xhr.status == 200) {
        alert("Sucess! Upload completed");
    } else {
        alert("Error! Upload failed");
    }
};
xhr.onerror = function() {
    alert("Error! Upload failed. Can not connect to server.");
};

progressBar.value = 0;
xhr.open("POST", "ajax-upload", true);
xhr.setRequestHeader("Content-Type", fileInput.files[0].type);
xhr.send(fileInput.files[0]);
}

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

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