Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to overwrite an old file with new one via ajax call in .Net MVC.
until now i can retreve data from DB but have no idea how to make overwrite.

Can somebody help me here?

What I have tried:

Here is my Controller:

[HttpPost]
    public JsonResult UpdateFile(int id, HttpPostedFileBase postedFile)
    {
        byte[] bytes;
        using (BinaryReader br = new BinaryReader(postedFile.InputStream))
        {
            bytes = br.ReadBytes(postedFile.ContentLength);
        }

        using (FileDBEntities db = new FileDBEntities())
        {

            tblFile fupt = db.tblFiles.Where(x => x.id == id).FirstOrDefault();
            fupt.Name = Path.GetFileName(postedFile.FileName);
            fupt.ContentType = postedFile.ContentType;
            fupt.Data = bytes;

            db.SaveChanges();
            return Json(new { success = true }, JsonRequestBehavior.AllowGet);
        }
    }


and my javascript:

function loadFileData() {
$.ajax({
    type: "GET",
    url: "/File/FileIndex",
    dataType: "JSON",
    success: function (data) {
        $.each(data, function (i, val) {
            var trow = $('<tr/>');
            var trowb = $('<tr/>').data("id", val.id);
            trow.append('<td colspan="2">' + val.Name + " " + '</td>');
            trowb.append('<td><input style="width:250px;" type="file" id="choose" /></td><td><input  type="button" value="upload" id="upload" /></td>');
          
            tab.append(trow);
            tab.append(trowb);
        });
        $("#showFiles").html(tab);
    },
    error: function () {
        alert("Failed! Please try again.");
    }
});
var tab = $('<table style="width:300px" border=1 class=MyTable></table>');


tab.on("click", "#upload", function (e) {
    //$('#uploadStatus').html("ok");
    var tr = $(this).closest("tr");
    var id = tr.data("id");
    var input = $('#choose').file;
    
  
    $.ajax({
        type: "POST",
        url: "/File/UpdateFile",
        dataType: "JSON",
        data: {
            id: id,
            pospostedFile: input
        },
        success: function (data) {
            $('#uploadStatus').html("ok");
            loadFileData();
        },
        error: function () {
            alert("Failed! Please try again.");
        }
    });

});


}


Debbuger says me that I'm sending "null" as "postedFile". How can I take this value from input and feed it to my Controller?
Posted
Comments
F-ES Sitecore 30-Jul-20 7:28am    
You can't upload a file via ajax like that. google for how you upload a file via ajax, it's very well documented.
Member 14803832 30-Jul-20 14:04pm    
Maybe You have some useful links, I'm googling since a week, but can't find anything what can be useful in my case. I've done it already without ajax, but cant go further
F-ES Sitecore 30-Jul-20 15:18pm    
This was the first one when I goggled "c# upload file ajax", I'm sure there are other better ones

https://stackoverflow.com/questions/35783168/jquery-ajax-uploading-a-file-and-other-values-from-client-side

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900