How to download file in browser from physical path

1 Answer 38 Views
Upload
Danish
Top achievements
Rank 1
Danish asked on 21 Dec 2021, 04:42 AM

Hello, I'm currently working on a project for my company and ive encountered a problem. I dont know why I cant download the file attachment. ive attached a jpg file of the screenshot for you to see. There is a file attachment named sad kermit(2).jpg and a message in the console in the screenshot

This is my kendo form code in view.

$("#form").kendoForm({
            validatable: { validationSummary: true },
            orientation: "horizontal",
            formData: {
                ID: "@Model.ID",
                IssueNumber: "@Model.ISSUE_NUM",
                Title: "@Model.TITLE",
                Environment: "@Model.ENVIRONMENT",
                Application: "@Model.APPLICATION",
                Module: "@Model.MODULE",
                Priority: "@Model.PRIORITY",
                Status: "@Model.STATUS",
                FID: "@Model.FID",
                Jobname: "@Model.JOBNAME",
                Username: "@Model.USERNAME",
                MantisNumber: "@Model.MANTIS_NO",
                ModifiedBy: "@ViewBag.User_Name",
                Upload: "",
                ModifiedDate: "@Model.MODIFIED_DATE",
                Description: "@Model.DESCRIPTION",
                IssueType: "@Model.ISSUE_TYPE",
                IssueCategory: "@Model.ISSUE_CATEGORY",
                IssueResolution: "@Model.ISSUE_RESOLUTION",
                Remarks: "@Model.REMARKS",
                AssignedTo: "@Model.ASSIGNED_TO",
                CreatedBy: "@ViewBag.User_Name"
            },
            items: [{
                type: "group",
                label: "Edit Issue Details",
                items: [
                    ...,
                   ...,
                    ...,
                    ...,
                    ...,
                   ...,
                    ...,
                    ...,
                    ...,
                    ...,
                    ...,
                    {
                        field: "Upload",
                        label: "Upload File:",
                        editor: function (container, options) {
                            $("<input name='files' id='files' type='file' aria-label='files' />").appendTo(container).kendoUpload({
                                async: {
                                    saveUrl: '@Url.Action("UploadFiles", "Issue")',
                                    removeUrl: '@Url.Action("RemoveFiles", "Issue")',
                                    autoUpload: true
                                },
                                files: uploads
                            });
                        }
                    },
                    ...,
                    ...,
                    ...,
                    ...,
                    ...,
                    ...,
                    ...,
                    ...,
                ]
                }],
                submit: function (ev)...
            });

this is the code I use to try to download the file attachment in the same view but it isnt working I guess

$(".k-file").click(function (e) {
                var filename = $(this).find(".k-file-name").html();

                $.ajax({
                    type: "POST",
                    data: { "name": filename },
                    url: "/Issue/DownloadFile",
                    success: function (res) {
                        if (res.Success) {
                            console.log(res.DownloadUrl);
                            window.open(res.DownloadUrl, '_blank');
                        }
                    }
                });
            });
this is the controller 
[HttpPost]
        public ActionResult DownloadFile(string name)
        {
            var folderName = Session["IssueNum"] as string;

            string fileDirectory = Path.Combine(System.Web.HttpContext.Current.Request.PhysicalApplicationPath, "App_Data", folderName, name);

            return Json(new
            {
                Success = true,
                DownloadUrl = fileDirectory
            }, JsonRequestBehavior.AllowGet);
        }
Could you suggest me a solution? Thank you

1 Answer, 1 is accepted

Sort by
0
Ianko
Telerik team
answered on 22 Dec 2021, 02:32 PM

Hello Danish,

The approach you are using is correct and the error you get is related to the browser and not the Kendo Form or Kendo Upload. I assume you are returning a physical path and instead you should return the proper URL that would enable the server to return the resource, e.g., "https://your.domain.com/images/yourimage.jpg". For example: https://dojo.telerik.com/@iankodj/oZUwOzUB.

Here you a SO thread on the topic: https://stackoverflow.com/questions/39007243/cannot-open-local-file-chrome-not-allowed-to-load-local-resource

Regards,
Ianko
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Upload
Asked by
Danish
Top achievements
Rank 1
Answers by
Ianko
Telerik team
Share this question
or