Hatena::ブログ(Diary)

hayashi_77の日記

2010-01-28 jQuery.upload

jQuery.upload

18:17

jQuery.upload

  <script type="text/javascript">
    $(function() {
        $('#upload_button').click(function() {
            $('#fields').upload('/jquery/upload/demo_upload.json', function(res) {
                alert(
                    'Field1: ' + res.field1 + "\n" + 
                    'Field2: ' + res.field2 + "\n" +
                    'Filename: ' + (res.file ? res.file.name : '')
                );
            }, 'json');
        });
    });
  </script>
  <form action="#" method="post">
    <div id="fields">
      <label>Field1</label><input name="field1" value="" type="text"><br>
      <label>Field2</label><input name="field2" value="" type="text"><br>
      <input name="file" type="file"><br>
    </div>
    <input value="Submit" id="upload_button" type="button">
  </form>

エラーが出た

Firefoxではjsonを表示すると勝手に<pre></pre>が入り、IEではダウンロードになる

解決

ImportAction.java

    public ActionResult index() throws IOException {
    	JsonProvider jsonProvider = ProviderFactory.get(JsonProvider.class);
    	try {
			userService.importUsers(csv);
		} catch (Exception e) {
			json = jsonProvider.toJson(getErrorJsonMap(e));
			return new Forward("../json.jsp");
		}
		json = jsonProvider.toJson(getSuccessJsonMap());
		return new Forward("../json.jsp");
    }

json.jsp

<body>
<pre>${action.json}</pre>
</body>

jquery.upload.js

96行目を修正
data = $(contents).find('body').find('pre').html();
↓
data = $(contents).find('body').html();
トラックバック - http://d.hatena.ne.jp/hayashi_77/20100128/1264670234