Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
On click of any Image I need to upload the new file selected. But I get error in my script

var file1 = $(this).file[0];


My View :

@using (@Html.BeginForm("uploadProductImage", "SellereLogin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <table id="listOfProduct">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ProductId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Image1Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Image2Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Image3Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Image4Id)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>

                    @Html.DisplayFor(modelItem => item.ProductId)

                </td>
                <td>

                    @Html.HiddenFor(m => item.Image1Id)
                    <img src ="@System.Configuration.ConfigurationManager.AppSettings["prodImage"]" class="click"/>

                </td>
                <td>

                    @Html.HiddenFor(m => item.Image2Id)
                    <img src ="@System.Configuration.ConfigurationManager.AppSettings["prodImage"]" class="click"/>

                </td>
                <td>

                    @Html.HiddenFor(m => item.Image3Id)
                    <img src ="@System.Configuration.ConfigurationManager.AppSettings["prodImage"]" class="click"/>

                </td>
                <td>

                    @Html.HiddenFor(m => item.Image4Id)
                    <img src ="@System.Configuration.ConfigurationManager.AppSettings["prodImage"]" class="click"/>
                    @*@Html.ActionLink("Change", "uploadProductImage", new { productId = item.ProductId, imageId = item.Image4Id })*@

                </td>
            </tr>
        }

    </table>
    <div>
        <input type="file" id="file1" style="display: none" />
    </div>
}


My Script :

<script>
       $(function () {
           $('.click').on('click', function () {
               $('#file1').trigger('click');
           });

           $('#file1').on('change', function () {
               debugger;
               var file1 = $(this).file[0];
               var formData = new FormData($('form')[0]);

               $.ajax({
                   type: "POST",
                   url: "SellereLogin/uploadProductImage",
                   enctype: 'multipart/form-data',
                   data: {
                       file: file1,
                       productId: 123,
                       imageId: 12345
                   },
                   success: function () {
                       alert("Data Uploaded: ");
                   }
               });
           });
       });
   </script>
Posted
Comments
Bernhard Hiller 30-Jul-14 2:20am    
"But I get error in my script" and what's the error message?

1 solution

Try:

JavaScript
var file1 = this.file[0];


You shouldn't need a selector since we're dealing with pure javascript there. The selector is primarily for DOM traversal, and within the functional code you've already hooked the pertinent DOM element.
 
Share this answer
 
v2

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