Issue in image upload

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 11 ans
Hi all,

I'm going to add new function for tryout image for my store. Image upload is work. But when i show new image in in the view, it shows my previous image. I'll post my code here. Please give me help


[NopHttpsRequirement(SslRequirement.Yes)]
        public ActionResult TryOutPhoto()
        {
            if (!IsCurrentUserRegistered())
                return new HttpUnauthorizedResult();

            //if (!_customerSettings.AllowCustomersToUploadAvatars)
            //    return RedirectToAction("MyAccount");

            var customer = _workContext.CurrentCustomer;

            var model = new CustomerTryOutImageModel();
            model.NavigationModel = GetCustomerNavigationModel(customer);
            model.NavigationModel.SelectedTab = CustomerNavigationEnum.TryOutImage;
            string url=_pictureService.GetPictureUrl(
                customer.GetAttribute<int>(SystemCustomerAttributeNames.TryOutImageId),
                _mediaSettings.TryOutPictureSize,
                false);

            if (url == string.Empty)
            {
                ViewBag.IsTryOutExist = "There isn't an image, please upload your image";
            }

            model.TryOutImageUrl = url;

            return View(model);
        }

        [HttpPost, ActionName("TryOutPhoto")]
        [FormValueRequired("upload-tryoutimage")]
        public ActionResult UploadTryOutImage(CustomerTryOutImageModel model, HttpPostedFileBase uploadedFile)
        {
            if (!IsCurrentUserRegistered())
                return new HttpUnauthorizedResult();

            //if (!_customerSettings.AllowCustomersToUploadAvatars)
            //    return RedirectToAction("MyAccount");

            var customer = _workContext.CurrentCustomer;

            model.NavigationModel = GetCustomerNavigationModel(customer);
            model.NavigationModel.SelectedTab = CustomerNavigationEnum.TryOutImage;


            if (ModelState.IsValid)
            {
                try
                {
                    var customerTryOutImage = _pictureService.GetPictureById(customer.GetAttribute<int>(SystemCustomerAttributeNames.TryOutImageId));
                    if ((uploadedFile != null) && (!String.IsNullOrEmpty(uploadedFile.FileName)))
                    {
                        int tryOutMaxSize = _customerSettings.TryOutMaximumSizeBytes;
                        if (uploadedFile.ContentLength > tryOutMaxSize)
                            throw new NopException(string.Format("Maximum tryout image size is {0} bytes", tryOutMaxSize));

                        byte[] customerPictureBinary = uploadedFile.GetPictureBits();
                        if (customerTryOutImage != null)
                            customerTryOutImage = _pictureService.UpdatePicture(customerTryOutImage.Id, customerPictureBinary, uploadedFile.ContentType, null, true);
                        else
                            customerTryOutImage = _pictureService.InsertPicture(customerPictureBinary, uploadedFile.ContentType, null, true);
                    }

                    int customerTryOutImageId = 0;
                    if (customerTryOutImage != null)
                        customerTryOutImageId = customerTryOutImage.Id;

                    _customerService.SaveCustomerAttribute<int>(customer, SystemCustomerAttributeNames.TryOutImageId, customerTryOutImageId);

                    model.TryOutImageUrl = _pictureService.GetPictureUrl(
                        customer.GetAttribute<int>(SystemCustomerAttributeNames.TryOutImageId),
                        _mediaSettings.TryOutPictureSize,
                        false);

                  

                    return View(model);
                }
                catch (Exception exc)
                {
                    ModelState.AddModelError("", exc.Message);
                }
            }


            //If we got this far, something failed, redisplay form
            model.TryOutImageUrl = _pictureService.GetPictureUrl(
                customer.GetAttribute<int>(SystemCustomerAttributeNames.TryOutImageId),
                _mediaSettings.TryOutPictureSize,
                false);
            return View(model);
        }
  


@using (Html.BeginForm("TryOutPhoto", "Customer", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    <div class="account-page">
        <div class="page-title">
            <h1>@T("Account.MyAccount") - @T("Account.TryOutPhoto")</h1>
      
       <span style="color:#E84040; font-size:13px;">@ViewBag.IsTryOutExist</span>
        </div>
        <div class="clear">
        </div>
        <div class="body">
            <div class="customer-avatar">
                <div class="message-error">
                    @Html.ValidationSummary(true)
                </div>
                <div class="clear">
                </div>
                        
                <div class="section-body">
                  
                    <input name="uploadedFile" type="file" />
                </div>
                <div class="clear">
                </div>
                <div class="buttons"><span class="round-button-block">
                    <button type="submit" name="upload-tryoutimage" value="upload-tryoutimage" class="uploadavatarbutton">@T("Common.Upload")</button></span>
                    @if (!String.IsNullOrEmpty(Model.TryOutImageUrl))
                    {
                        <span class="round-button-block"><button type="submit" name="remove-tryoutimage" value="remove-tryoutimage" class="uploadavatarbutton">@T("Account.Avatar.RemoveTryOutPhoto")</button></span>
                    }
                </div>
                 <div class="clear">
                </div>
                <div>
                 Please <a href="@Url.RouteUrl("CustomerTryOutImage")">click here</a> to refresh the page, if uploaded image doesn't appear in the screen.
                </div>
                <div class="clear">
                </div>
                    
                <div><br />
                    @T("Account.TryOutPhoto.UploadRules")</div>
                    <div class="clear">
                </div>
              
                                            <div class="pane clearfix">
                                              @if (!String.IsNullOrEmpty(Model.TryOutImageUrl))
                                              {
                                            <img src="@(Model.TryOutImageUrl)" alt="Try Out Image" />
                                              }                          
                                                <table class="coords">
                                                    <tr>
                                                        <td>
                                                            crop x
                                                        </td>
                                                        <td>
                                                            <input type="text" size="4" id="x1" name="x1" class="coor" readonly="readonly" visible="false" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            crop y
                                                        </td>
                                                        <td>
                                                            <input type="text" size="4" id="y1" name="y1" class="coor" readonly="readonly" visible="false" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            crop width
                                                        </td>
                                                        <td>
                                                            <input type="text" size="4" id="x2" name="x2" class="coor" readonly="readonly" visible="false" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            crop height
                                                        </td>
                                                        <td>
                                                            <input type="text" size="4" id="y2" name="y2" class="coor" readonly="readonly" visible="false" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            image width
                                                        </td>
                                                        <td>
                                                            <input type="text" size="4" id="w" name="w" class="coor" readonly="readonly" visible="false" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            image height
                                                        </td>
                                                        <td>
                                                            <input type="text" size="4" id="h" name="h" class="coor" readonly="readonly" visible="false" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            lock proportion
                                                        </td>
                                                        <td>
                                                            <input type="checkbox" checked="checked" />
                                                        </td>
                                                    </tr>
                                                </table>
                                            </div>
                                          
                                      <div class="buttons">
                                      @if (!String.IsNullOrEmpty(Model.TryOutImageUrl))
                                      {
                        <span class="round-button-block"><button type="submit" name="crop-tryoutimage" value="crop-tryoutimage" class="uploadavatarbutton">@T("Account.TryOutPhoto.Crop")</button></span>
                                      }
                                      </div>
                    
                              
                              
                            
                                
  
                    
                  
                      
          
        </div>
    </div>

    </div>
}
Il y a 11 ans
1) check wether the thumbnil and other size images created on the content/images/thumbs folder.
2) check your display order of the image
Il y a 11 ans
It was due to TryOutPictureSize=0

model.TryOutImageUrl = _pictureService.GetPictureUrl(
                        customer.GetAttribute<int>(SystemCustomerAttributeNames.TryOutImageId),
                        _mediaSettings.TryOutPictureSize,
                        false);
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.