2012-03-20 2 views
2

forum 회원 extjs 4 및 JAVA의 파일 업로드에 문제가 있습니다. 내가 extjs4 및 업로드 양식 코드를 사용하고extjs로 이미지 파일 업로드 4 spring

버튼 업로드 이미지 작용을

{ 
            xtype: 'filefield', 
            margin: '10 0 0 5', 
            width: 296, 
            fieldLabel: 'Image', 
            emptyText: 'Select Company logo...', 
            id: 'cmplogo', 
            itemId: 'cmplogo', 
            name: 'cmplogo', 
            labelWidth: 70 
           }, 
           { 
            xtype: 'button', 
            margin: '10 0 0 90', 
            width: 89, 
            text: 'Upload Image', 
            action: 'btn-upload-img' 
           }, 

입니다 :

fileUpload: function(btn) { 
     var win = btn.up('window'); 
     var form = win.down('form').getForm(); 
     alert('VALUE IS :'+form.getValues()); 

     if(form.isValid()){ 
      form.submit({ 
       url : 'company/UploadFile.action', 
       waitMsg: 'Uploading your file...', 
       success: function(fp, o) { 
        Ext.Msg.alert('Success', 'Your file has been uploaded.'); 
       } 
      }); 
     } 
    }, 

아래에 정의 된 기능을 가진 'btn을 업로드-IMG를'내 자바 컨트롤러는 아래 기능 코드를 가짐

@RequestMapping(value = "/company/UploadFile.action") 
    public @ResponseBody 
    Map<String, ? extends Object> uploadFile(UploadItem uploadItem, BindingResult result) throws Exception { 
     System.out.println("QUERY TO UPLOAD FILE"); 
     try { 
      if(result.hasErrors()) { 
       for(ObjectError error: result.getAllErrors()) { 
        System.err.println("Error: "+error.getCode()+" - "+error.getDefaultMessage()); 
       } 
      } 
      else 
      { 
       MultipartFile file = uploadItem.getFileData(); 
       String fileName = null; 
       InputStream inputStream = null; 
       OutputStream outputStream = null; 
       if(file.getSize() > 0) { 
        inputStream = file.getInputStream(); 
        if(file.getSize() > 10000) { 
         System.out.println("FILE SIZE:::"+file.getSize()); 
        } 
        System.out.println("SIZE ::"+file.getSize()); 
        fileName = "E:\\images\\"+file.getOriginalFilename(); 
        outputStream = new FileOutputStream(fileName); 
        System.out.println("FILE NAME AND PATH IS ::"+fileName); 
        System.out.println("FILENNAME ::"+file.getOriginalFilename()); 

        int readBytes = 0; 
        byte[] buffer = new byte[10000]; 
        while((readBytes = inputStream.read(buffer, 0, 10000)) !=-1) { 
         outputStream.write(buffer, 0, readBytes); 
        } 
        outputStream.close(); 
        inputStream.close(); 
       } 
      } 


     } catch (Exception e) { 
      return getModelMapError("Error trying to read city"); 
     } 
     return null; 
    } 

위의 코드에 무엇이 잘못되었는지 알 수 있습니다. 내 불을 지르고 콘솔

**uncaught exception: You're trying to decode an invalid JSON String: <pre>{"message":"Error trying to read city","success":false}</pre>** 

나에게 내 오류가 곧 해결 얻을 시도 할 수있는 몇 가지 솔루션을 제안 해주십시오 오류가 아래에 나와 있습니다.

+0

이 경우 해결책을 알아낼 당신이 수 있었습니까? – pacman

답변

3

컨트롤러가 extj가 지원하지 않는 응답으로 JSON 데이터 (application/json)를 전송합니다. 응답 내용 유형을 "text/html"로 설정해보십시오. 정상적으로 작동합니다. 변경 @ResponseBody에 ResponseEntity

@RequestMapping(value = "/company/UploadFile.action") 
public ResponseEntity<String> Map<String, ? extends Object> uploadFile(UploadItem uploadItem, BindingResult result) throws Exception { 
    HttpHeaders responseHeaders = new HttpHeaders(); 
    responseHeaders.add("Content-Type", "text/html; charset=utf-8"); 

    // your controller logic goes here 

    return new ResponseEntity<String>(response, responseHeaders, HttpStatus.OK); 
} 
+1

다른 서버 (피라미드에서 사용하는 서버 붙여 넣기)와 동일한 문제가 발생했으며 digz6666의 답변 (콘텐츠 유형 부분)은 http://www.sencha.com/forum/showthread에서 확인되었습니다. .php? 120201- 양식을 제출하는 것 같습니다. 응답 형식을 잘못 해석하는 것 같습니다. & p = 557527 & viewfull = 1 # post557527 – Walter

0

Spring을 사용하여 extjs 4 이외의 파일을 업로드하려면 restTemplate 또는 Apache HttpClient with multipartData를 사용할 수 있습니다. Spring은 또한 파일 업로드를위한 내장 된 기능을 제공합니다.

유용한 링크 -

http://blog.springsource.org/2009/03/27/rest-in-spring-3-resttemplate/

+0

이미지를 업로드 할 수는 있지만 이미지 xtype에서 이미지 업로드를 표시 할 수없는 이유를 모르겠습니다. 방화범이 끌려서 이미지가 업로드되고 완벽하게 읽을 수 있습니다. 하지만 그게 왜 보이지 않는가 큰 문제 야. –

+0

"E : \\ images \\ YOUR_FILE_NAME"에 파일을 저장할 수 있었습니까? –

+0

예 E : 이미지 폴더의 디렉토리 –

-1

당신은 당신이 방화범 오류가보고있는 문자열을 반환 자바 쪽, 어딘가에서 예외를 던지고있다. catch 블록에 중단 점을 추가하고 예외가 무엇인지 조사하는 것이 좋습니다. 그러면 깨진 내용과 해결 방법을 더 잘 이해할 수 있습니다.

+0

여기서 무엇이 잘못되었는지를 이해할 수 없으므로 http://loianegroner.com/2011/07/extjs-4-file-upload-spring-mvc-3-example/ 자습서를 구현하려했습니다. 그러나 나는 그것을 작동시킬 수 없다. –

+0

@CodeChimp : 그의 응답에 오류가 포함되어 있다는 사실은 여기서 문제가되지 않는다. JavaScript가 ExtJS에 의해 응답이 제대로 처리되지 않는다는 사실이다. – Walter

0

비슷한 문제가 발생합니다.

JSON으로 응답을 디코딩하려고 시도 할 때 ExtJS로 범위를 좁혔습니다. 그러나 응답은 <pre> 태그로 싸여 있습니다. 그러나 내가 방화범을 쳐다볼 때 나는 단지 평범한 json 문자열을 본다. 그것은 장면 브라우저 뒤에 일반 multipart 양식 게시물이기 때문에 어떻게 든 <pre> 태그로 응답을 래핑합니다.

이 브라우저 동작을 수정하는 방법을 모르겠 음.

0

또한 단지 다음과 같이 "*/*"와 MappingJacksonHttpMessageConverter의 supportedMediaTypes 속성을 설정할 수 있습니다 :

<mvc:annotation-driven> 
    <mvc:message-converters> 
     <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> 
     <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
      <property name="supportedMediaTypes" value="*/*" /> 
     </bean> 
    </mvc:message-converters> 
</mvc:annotation-driven> 
관련 문제