Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
Upload file failed to save in Database

public JsonResult SaveAttachment(string buyerCode) { Dictionary<int, CheckSessionData> IDictionary = CheckSessionData.GetSessionValues(); long companyId = (long)IDictionary[1].Id; long userId = (long)IDictionary[3].Id;

        var buyer = DB.Buyers.FirstOrDefault(x => x.Code == buyerCode);
        string basePath = "~/Upload/BuyerAttachment/";
        string path = Server.MapPath(basePath);
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        HttpFileCollectionBase files = Request.Files;
        List<string> filePathList = new List<string>();
        for (int i = 0; i > files.Count; i++)
        {
            HttpPostedFileBase file = files[i];
            var date = DateTime.Now.ToString().Replace('/', '_');
            string FilePath = Path.Combine(path, date.Replace(':', ' ') + file.FileName);
            file.SaveAs(FilePath);
            filePathList.Add(FilePath);
        }
        foreach (var item in filePathList)
        {
            var buyerAttachment = new Buyer_Attachment();
            buyerAttachment.BuyerId = buyer.BuyerId;
            buyerAttachment.DateOfEntry = DateTime.Now;
            buyerAttachment.EntryBy = userId;
            buyerAttachment.AttachmentURL = path;
            DB.Buyer_Attachment.Add(buyerAttachment);
        }

        try
        {
            DB.SaveChanges();
        }
        catch (Exception)
        {
            return Json(false, JsonRequestBehavior.AllowGet);
        }
        return Json(true);
    }
}

function SaveAttachment(code) { var files = $("#CustomerAttachment").get(0).files;

        var fileData = new FormData();

        for (var i = 0; i < files.length; i++) {
            fileData.append("fileInput", files[i]);
        }

        $.ajax({
            type: "POST",
            url: "/BuyerSimple/SaveAttachment?buyerCode="+code,
            dataType: "json",
            contentType: false, // Not to set any content header
            processData: false, // Not to process data
            data: fileData,
            success: function (result, status, xhr) {
                filePathList = result;
                $('#AttachmentPathList').val(result);
            },
            error: function (xhr, status, error) {
                alert(status);
            }
        });
    }
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!