Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a last attempt for me to get some insight on this issue before I scratch JqGrid all together. I have a col in my colModel to upload an Image. I am using ajaxfileupload. Although there are several of the same examples out there they seem to work for others but mine is not. When I select an Image from the enctype: "multipart/form-data" for some reason it is not putting anything in the record for the object. I have a few breakpoints and when I view the data everything is visible but the product Image. So all fields are there and ProductImage is null. It is like JqGrid is not holding the object and passing it. Since it is null, nothing gets uploaded either. Maybe I am missing something but I have looked through my code over and over again and it appears to be just like what I have read in examples that supposedly work.


Any help with would be fantastic as I am ready to scratch this and use something else.

Below is my code for the process. sections only. if you need to see other code let me know.

What I have tried:

JqGrid:
JavaScript
                {
                    name: "ProductImage",
                    index: "ProductImage",
                    mtype: "Post",
                    editable: true,
                    editrules: { required: true },
                    edittype: "file",
                    search: true,
                    resizable: false,
                    width: 210,
                    align: "left",
                    editoptions: {
                        enctype: "multipart/form-data"
                    }
                },
.....

            {
                // edit option
                zIndex: 100,
                url: "/Admin/EditProducts",
                closeOnEscape: true,
                closeAfterEdit: true,
                recreateForm: true,
                afterSubmit: uploadImage,
                afterComplete: function (response) {
                    if (response.responseText) {
                        alert(response.responseText);
                    }
                }

            },

....

    function uploadImage(response, postdata) {
        //debugger;
        //var json = $.parseJSON(response.responseText);
        //if (json) return [json.success, json.message, json.id];
        //return [false, "Failed to get result from server.", null];
        var data = $.parseJSON(response.responseText);

        if (data.success == true) {
            if ($("#ProductImage").val() != "") {
                ajaxFileUpload(data.id);
            }
        }

        return [data.success, data.message, data.id];
    }

    function ajaxFileUpload(id) {

        $.ajaxFileUpload(
            {
                url: "/Admin/UploadImage",
                secureuri: false,
                fileElementId: "ProductImage",
                dataType: "json",
                data: { id: id },
                success: function (data, status) {

                    if (typeof (data.isUploaded) != "undefined") {
                        if (data.isUploaded == true) {
                            return;
                        } else {
                            alert(data.message);
                        }
                    }
                    else {
                        return alert("Failed to upload image!");
                    }
                },
                error: function (data, status, e) {
                    return alert("Failed to upload image!");
                }
            }
        )

        return false;
    }



Controller:

C#
        public string EditProducts(Product product)
        {
            string msg;
            try
            {
                if (ModelState.IsValid)
                {
                    using (StoreEntities db = new StoreEntities())
                    {
                        db.Entry(product).State = EntityState.Modified;
                        db.SaveChanges();
                        msg = "Saved Successfully";
                    }
                }
                else
                {
                    msg = "Did not save! ";
                }
            }
            catch (DbEntityValidationException ex)
            //catch (Exception ex)
            {
                msg = "Error occured:" + ex.Message;
                foreach (var validationErrors in ex.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        System.Diagnostics.Debug.WriteLine("Property: {0} Error: {1}",
                                   validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
            return msg;
        }

....

        #region Upload Images

        [HttpPost]
        public JsonResult UploadImage(HttpPostedFileBase ProductImage)
        {
            string directory = "~/Images/";

            if (ProductImage != null && ProductImage.ContentLength > 0)
            {
                var fileName = Path.GetFileName(ProductImage.FileName);
                ProductImage.SaveAs(Server.MapPath(Path.Combine(directory, fileName)));
                //ProductImage.SaveAs(Path.Combine(directory, fileName));
            }

            return Json(new { isUploaded = true, message = "Uploaded Successfully" }, "text/html");
        }

        #endregion
Posted
Comments
[no name] 28-Aug-18 10:57am    
Maybe if you explained "what" you want to do.

Using a "grid" to "upload images" is a concept foreign to many.

Filenames, thumb nails, links we get.
Member 13812021 28-Aug-18 15:51pm    
I am using jqgrid to show my records. Since it has the capability of adding and editing also I thought it was the clean way to go. Ultimately I would like to add a record with a image link included with the product. The link would be where the file Upload creates the folder and image storage preferably the category that it is in. So create a folder inside of the images folder (if not there already) and put the photo in it. I believe that I can handle the thumbnail with just Css when the page is loaded. But if you think, for display purposes it would be better to load a thumbnail instead than I would probably have to agree that it would make load time quicker. Also not looking for inline editing either. The popup provided is good enough.
Member 13812021 29-Aug-18 23:14pm    
I have gotten the file to upload but I am still not able to save the file name in the database that it uploads. So when I upload an image I would like the record to save the filename in the column ProductImage

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