我的Blog我做主^_^

          走向一條通往JAVA的不歸路...

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            64 隨筆 :: 68 文章 :: 77 評論 :: 0 Trackbacks

          用于解決在一個頁面上傳多個文件的問題.

          <script type="text/javascript">
          function showDialog(url){
          ?//彈出一個showModalDialog,并以returnValue來獲取返回值
          ?var returnValue = window.showModalDialog(url);
          ?//alert(returnValue);
          ?if(returnValue!=null){
          ? //for(var i=0;i<returnValue.length;i++){
          ?? //document.all.info.innerHTML = returnValue[i]+"<br>";
          ? //}
          ? //輸出返回值
          ? document.getElementById("IMAGENAME").value = document.getElementById("IMAGENAME").value + returnValue + "|";
          ? //document.all.imgname.innerHTML=returnValue;
          ?}
          }
          </script>

          上傳示例






          在示例圖中的"上傳圖片"是一個button按鈕,給它一個onclick事件,當點擊時觸發:
          <td height="25">
          ?????????<input name="IMAGENAME" type="text" id="IMAGENAME" />
          ?????????<input type="button" name="Submit3" value="上傳圖片" onclick="showDialog('automobile/up.jsp')" />
          </td>
          彈出圖片上傳頁:


          圖片上傳頁:
          <base target="_self"/>
          <body>
          <form action="/qcbst/v_uploadautomobileimage" name="myform" method="post" enctype="multipart/form-data" >
          <table width="100%"? border="0" align="center">
          ? <tr>
          ??? <td colspan="2">圖片上傳</td>
          ? </tr>
          ? <tr>
          ??? <td width="30%"><div align="right">汽車圖片:</div></td>
          ??? <td width="70%"><input type="file" name="imgname" size="20"><input type="submit" value="上傳"></td>
          ? </tr>
          ? <tr>
          ??? <td><div align="right">上傳信息:</div></td>
          ??? <td>&nbsp;</td>
          ? </tr>
          </table>
          </form>
          </body>

          當點擊上傳后.圖片上傳到服務器,然后,關閉當前頁,返回圖片的名稱給調用頁:

          上傳servlet:
          public void doGet(HttpServletRequest request, HttpServletResponse response)
          ???throws ServletException, IOException {

          ??response.setCharacterEncoding("GBK");
          ??response.setContentType("text/html");
          ??UploadBean up = new UploadBean();//--------文件上傳類
          ??String path = request.getRealPath(request.getContextPath()).substring(0,request.getRealPath(request.getContextPath()).lastIndexOf("\\"));
          ????
          ???? try{
          ???????? File file = new File(path+"/img/automobile");//
          ???????? if(!file.exists()){
          ?????????? file.mkdir();
          ???????? }
          ???? }catch(Exception e){???? ?
          ???? }
          ???? up.setObjectPath(path+"/img/automobile/");//
          ???? //設置上傳文件大小
          ???? up.setSize(10000*1024);
          ???? //可上傳文件后綴名
          ???? up.setSuffix(".JPG.JPGE.jpg.jpge.gif.GIF");
          ???? try{
          ???? ?up.setSourceFile(request);
          ???? }catch(Exception e){
          ???? ?response.getWriter().write("圖片上傳失?。。?!");
          ???? }
          ???? String [] saSourceFile = up.getSourceFile();
          ???? String [] saObjectFile = up.getObjectFileName();
          ???? String [] saDescription = up.getDescription();
          ????
          ???? int iCount = up.getCount();
          ???? String sObjectPath = up.getObjectPath();
          ????
          ???? if(saDescription != null && saDescription[0].indexOf("ERR") == -1){
          ????? response.getWriter().write("<script language='javascript'>window.returnValue = '");
          ????? response.getWriter().write(saObjectFile[0]);
          ????? response.getWriter().write("';window.close();</script>");

          ???? }else{
          ???? ?
          ???? ?response.getWriter().write("圖片上傳失?。。。?);
          ???? }
          ?}
          ?上傳完成后:
          OK,完成了!!!



          posted on 2007-01-16 16:17 java_蟈蟈 閱讀(2132) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 连云港市| 武强县| 乾安县| 河北省| 莱西市| 望谟县| 吴旗县| 东源县| 瑞安市| 铁岭市| 泸水县| 西乌| 郸城县| 金湖县| 洛隆县| 马鞍山市| 安西县| 安溪县| 平远县| 福清市| 什邡市| 庆阳市| 牙克石市| 饶平县| 嘉义市| 介休市| 双城市| 安阳市| 思南县| 勐海县| 达孜县| 临夏市| 镇原县| 黄平县| 汉源县| 东平县| 长岛县| 清徐县| 龙陵县| 吴江市| 兴城市|