Skip to content

Tutorial: How to Upload Image Files from OrgChart JS to the server - ASP.NET Core

Notifications You must be signed in to change notification settings

WasimAhmad/OrgChartUploadImageNETCore

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tutorial: How to Upload Image Files from OrgChart JS to the server - ASP.NET Core

To upload image from the edit form you have to implement imageuploaded event hadler. Here is an example

    chart.editUI.on('imageuploaded', function (sender, file, input) {
        var formData = new FormData();
        formData.append('file', file);           

        $.ajax({
            type: "POST",
            url: "@Url.Action("UploadImage")",
            data: formData,
            dataType: 'json',
            contentType: false,
            processData: false,
            success: function (data) {
                input.value = data.url;
            },
            error: function (error) {
                alert(error);
            }
        });
    });

imageUploadHandler has 3 parameters:

  • sender - the editUI object
  • file - the actual file that is going to be uploaded
  • input - this is the HTML input element from the edit form

The next step is to create upload method on the server

    [HttpPost]
    public async Task<IActionResult> UploadImage(IFormFile file)
    {
        var uniqueFileName = GetUniqueFileName(file.FileName);
        var uploads = Path.Combine(hostingEnvironment.WebRootPath, "uploads");
        var filePath = Path.Combine(uploads, uniqueFileName);
        
        if (file.Length > 0)
        {
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }
        }

        return Ok(new { url = Url.Content("~/uploads/" + uniqueFileName) });
    }

BALKANGraph

About

Tutorial: How to Upload Image Files from OrgChart JS to the server - ASP.NET Core

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 73.7%
  • HTML 26.3%