Dima Alek

Dima Alek

  • NA
  • 43
  • 1.9k

How to overwrite file in database with ajax

Jul 31 2020 3:29 AM
I'm trying to replace some file in my DB using ajax call. I can not understand how to pass data(file) from my tag into controller, maybe somebody can give me an advise...
 
My controller:
  1. [HttpPost]  
  2. public JsonResult UpdateFile(int id, HttpPostedFileBase postedFile)  
  3. {  
  4.    byte[] bytes;  
  5.    using (BinaryReader br = new BinaryReader(postedFile.InputStream))  
  6. {  
  7.    bytes = br.ReadBytes(postedFile.ContentLength);  
  8. }  
  9.          using (FileDBEntities db = new FileDBEntities())  
  10.          {  
  11.                tblFile fupt = db.tblFiles.Where(x => x.id == id).FirstOrDefault();  
  12.                fupt.Name = Path.GetFileName(postedFile.FileName);  
  13.                fupt.ContentType = postedFile.ContentType;  
  14.                fupt.Data = bytes;  
  15.                db.SaveChanges();  
  16.                return Json(new { success = true }, JsonRequestBehavior.AllowGet);  
  17.       }  
  18. }  
my JS:
  1. function loadFileData() {  
  2.          $.ajax({  
  3.             type: "GET",  
  4.             url: "/File/FileIndex",  
  5.             dataType: "JSON",  
  6.             success: function (data) {  
  7.                $.each(data, function (i, val) {  
  8.                var trow = $('<tr/>');  
  9.                var trowb = $('<tr/>').data("id", val.id);  
  10.                trow.append('<td colspan="2">' + val.Name + " " + '</td>');  
  11.                trowb.append('<td><input style="width:250px;" type="file" id="choose" /></td><td><input type="button"                value="upload" id="upload" /></td>');  
  12.                tab.append(trow);  
  13.                tab.append(trowb);  
  14.             });  
  15.                   $("#showFiles").html(tab);  
  16.          },  
  17.             error: function () {  
  18.                   alert("Failed! Please try again.");  
  19.                }  
  20.          });  
  21.                   var tab = $('<table style="width:300px" border=1 class=MyTable></table>');  
  22.                   tab.on("click""#upload"function (e) {  
  23.                   //$('#uploadStatus').html("ok");  
  24.                   var tr = $(this).closest("tr");  
  25.                   var id = tr.data("id");  
  26.                   var input = $('#choose').file;  
  27.                            $.ajax({  
  28.                            type: "POST",  
  29.                            url: "/File/UpdateFile",  
  30.                            dataType: "JSON",  
  31.                            data: {  
  32.                            id: id,  
  33.                            pospostedFile: input  
  34.                            },  
  35.                                  success: function (data) {  
  36.                                  $('#uploadStatus').html("ok");  
  37.                                  loadFileData();  
  38.                            },  
  39.                            error: function () {  
  40.                                     alert("Failed! Please try again.");  
  41.                            }  
  42.                      });  
  43.                   });  
  44.             }  
Debbuger says me that I'm sending "null" as "postedFile". How can I take this value from input and feed it to my Controller?

Answers (4)