【问题标题】:jQuery Getting uploaded image's namejQuery获取上传图片的名称
【发布时间】:2015-10-22 16:58:29
【问题描述】:

//Picture upload
$(function() {
  $(":file").change(function() {
    if (this.files && this.files[0]) {
      var reader = new FileReader();
      reader.onload = imageIsLoaded;
      reader.readAsDataURL(this.files[0]);
    }
  });
});

function imageIsLoaded(e) {
  $('#myImg').attr('src', e.target.result);
};
<form id="form_roomtype" class="form-horizontal" style="padding-top: 57px;" action="roomtype" method="POST" enctype="multipart/form-data">
  <input type="hidden" id="current_roomtype_id" name="current_roomtype_id" value="0">
  <input type="hidden" id="roomtype_id_to_remove" name="roomtype_id_to_remove" value="0">
  <input type="hidden" id="chk_tarif_applicable" name="chk_tarif_applicable" value="0">
  <input type="hidden" id="photo_name" name="photo_name">

  <div>
    <span style="font-size: 16.85px; font-family: Arial, Helvetica, sans-serif; color: #757575;">ROOM PICTURE</span>
    <br />
    <br />
    <input type="file" name="file" id="file" />
    <br/>

    <img id="myImg" src="<%=request.getContextPath()%>/images/insert_images.png" alt="room image" height="175" width="285" onclick="file" />
    <br />Destination:
    <input type="text" value="C:\Project\Booking_Engine\Java\booking\src\main\webapp\data" name="destination" />
    </br>
    <br />
    <input type="submit" value="Upload" name="upload" id="upload" />
  </div>
</form>

我需要获取输入 (#file) 的 id,并使用顶部的图片上传 js 将其分配给具有 id (#photo_name) 的隐藏输入。

我需要获取 (&lt;input type="file" name="file" id="file" /&gt;) 的 id 并将其分配给 (&lt;input type="hidden" id="photo_name" name="photo_name"&gt;) 的 id。

我之前发的JS已经有了……我需要用它来分配id。

虽然 JS...(this.file[0])已经给了我上传文件的名称,但我已经有了输入文件的 ID...现在我需要的是取这个名称并将其分配给隐藏输入的 id。


我需要以下 java 代码中 photo_name 的值:

@覆盖 protected void doPost(HttpServletRequest 请求,HttpServletResponse 响应) 抛出 ServletException, IOException {

    String currentRoomTypeIdStr = request.getParameter("current_roomtype_id");
    Integer currentRoomTypeId = Integer.valueOf(currentRoomTypeIdStr);

    String photoName = request.getParameter("photo_name");

if (!Objects.equals(currentRoomTypeId, null) || !Objects.equals(roomtypeIdToRemove, null)) { 尝试 { mUserTransaction.begin();

            if (currentRoomTypeId == 0) { // Add mode
                ChambreTypeEntity typeChambreEntity = new ChambreTypeEntity();
                typeChambreEntity.setLibelle(inputTypeRoom);
                typeChambreEntity.setCode(inputCodeRoom);
                typeChambreEntity.setDescription(inputDescriptionRoom);
                typeChambreEntity.setNbMinPers(inputMinPerson);
                typeChambreEntity.setNbMaxPers(inputMaxPerson);
                typeChambreEntity.setNbEnfGratuit(inputChild);
                mEntityManager.persist(typeChambreEntity);


                ChambrePhotoEntity chambrePhotoEntity = new ChambrePhotoEntity();
                chambrePhotoEntity.setNomPhoto(photoName);
                chambrePhotoEntity.setTypeChambre(currentRoomTypeId);
                mEntityManager.persist(chambrePhotoEntity);

            }
            else { // Modification mode
                Query query = mEntityManager.createQuery("FROM ChambreTypeEntity WHERE id=:pId")
                        .setParameter("pId", currentRoomTypeId);
                List<ChambreTypeEntity> typeChambreEntityList = query.getResultList();
                if (!typeChambreEntityList.isEmpty()) {
                    ChambreTypeEntity typeChambreEntity = typeChambreEntityList.get(0);
                    typeChambreEntity.setLibelle(inputTypeRoom);
                    typeChambreEntity.setCode(inputCodeRoom);
                    typeChambreEntity.setDescription(inputDescriptionRoom);
                    typeChambreEntity.setNbMinPers(inputMinPerson);
                    typeChambreEntity.setNbMaxPers(inputMaxPerson);
                    typeChambreEntity.setNbEnfGratuit(inputChild);
                    mEntityManager.persist(typeChambreEntity);
                    mEntityManager.persist(chambreTypeTarifTypeEntity);

                    ChambrePhotoEntity chambrePhotoEntity = new ChambrePhotoEntity();
                    chambrePhotoEntity.setNomPhoto(photoName);
                    chambrePhotoEntity.setTypeChambre(currentRoomTypeId);
                    mEntityManager.persist(chambrePhotoEntity);


                }
            }
            mUserTransaction.commit();
        }

但是现在 photo_name 的值是 ""。我需要它是我在选择图片时选择的值。

【问题讨论】:

  • 对不起...你能澄清一下你在找什么
  • 我需要获取 () 的 id 并将其分配给 () 我已经有了我之前发布的 js...我需要使用它来分配 id 我已经通过 JS 获得了输入文件的 id。 . (this.file[0] 已经给了我上传文件的名称...现在我需要的是取这个名称并将其分配给隐藏输入的 id
  • 您希望在加载图像后交换 id 吗?
  • 是的,完全是@AswinRamakrishnan
  • 您希望图片的文件名/名称为valuephoto_name

标签: javascript java jquery


【解决方案1】:

以下是您要查找的内容吗?

//Picture upload
$(function() {
  // This is a better way of selecting the input field
  $("input[name^=file]").change(function() {
    if (this.files && this.files[0]) {
      var reader = new FileReader();
      reader.onload = imageIsLoaded;
      reader.readAsDataURL(this.files[0]);

      // This is what you should do
      $('#photo_name').val(this.files[0].name);
    }
  });
});

function imageIsLoaded(e) {
  $('#myImg').attr('src', e.target.result);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form_roomtype" class="form-horizontal" style="padding-top: 57px;" action="roomtype" method="POST" enctype="multipart/form-data">
  <input type="hidden" id="current_roomtype_id" name="current_roomtype_id" value="0">
  <input type="hidden" id="roomtype_id_to_remove" name="roomtype_id_to_remove" value="0">
  <input type="hidden" id="chk_tarif_applicable" name="chk_tarif_applicable" value="0">
  <input type="hidden" id="photo_name" name="photo_name">

  <div>
    <span style="font-size: 16.85px; font-family: Arial, Helvetica, sans-serif; color: #757575;">ROOM PICTURE</span>
    <br />
    <br />
    <input type="file" name="file" id="file" />
    <br/>

    <img id="myImg" src="<%=request.getContextPath()%>/images/insert_images.png" alt="room image" height="175" width="285" onclick="file" />
    <br />Destination:
    <input type="text" value="C:\Project\Booking_Engine\Java\booking\src\main\webapp\data" name="destination" />
    </br>
    <br />
    <input type="submit" value="Upload" name="upload" id="upload" />
  </div>
</form>

【讨论】:

  • 不,它不起作用。我想我没有解释清楚。我将编辑我的问题。对此我深表歉意。
  • 这是使用您提供的代码时返回的内容 当前文件 ID:file 文件新 ID:photo_name photo_name 当前 ID:photo_name photo_name 新 ID:file
  • 我已经更新了答案。浏览并添加文件后,检查photo_name 的值。
  • 是的,这正是我所需要的。感谢您的帮助
  • 不客气。我建议将标题更改为类似于“jQuery 获取上传的图像名称”的内容,因为现在似乎具有误导性。另外,将其标记为答案,以便对其他人有所帮助。谢谢!
猜你喜欢
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-30
  • 2020-03-10
相关资源
最近更新 更多