2011-05-03 56 views
0

我需要一些幫助,也都是通過jQuery在MVC 3獲取MVC使用jQuery發佈文件的MIME類型3

下面上傳從視圖文件的ContentType是認爲把手代碼帖子:

<div> 
    <label for="username">Username</label><br/> 
    <input type="text" name="username" id="username" value="" /><br/> 
    <img id="thumb" /><br/> 
    <a id="ajaxUpload" href="#">Upload Image</a><br/> 
    <div class="preview"></div> 
</div> 

@section JavascriptContent { 
<script type="text/javascript" src="/content/js/jquery/fileuploader.js"></script> 

<script type="text/javascript"> 
//<![CDATA[ 
    var thumb = $('img#thumb'); 

    var uploader = new qq.FileUploaderBasic({ 
     button: $('#ajaxUpload')[0], 
     enctype: 'multipart/form-data', 
     action: '@Url.Action("someAction", "someController")', 
     params: { }, 
     name: 'imageFile', 
     allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], 
     //each file size limit in bytes 
     //this option isnt supported in all browsers 
     sizeLimit: 4096000, // max size 
     minSizeLimit: 2048, // min size 
     multiple: false, 
     debug: true, 
     onSubmit: function(file, extension) { 
      this.params = { boardId: $('#someGuid').val(), username: $('#username').val() }; 
      $('div.preview').addClass('loading'); // Loading... 
     }, 
     onComplete: function(id, filename, response) { 
      thumb.load(function(){ 
       $('div.preview').removeClass('loading'); 
       thumb.unbind(); 
      }); 
      if (response.Success){ 
       thumb.attr('src', response.FilePath); 
      } else { 
       alert(response.Message); //json response that contains a message and some other info 
      } 
     } 
    }); 
//]]> 
</script> 
} 

,這裏是控制器動作,我需要的MIME類型的一部分[IE的部分工作正常]:

var stream = Request.InputStream; 
if (String.IsNullOrWhiteSpace(Request["qqfile"])) 
{ 
    // IE 
    HttpPostedFileBase postedFile = Request.Files[0]; 
    stream = postedFile.InputStream; 
    fileContentType = postedFile.ContentType; 
    fileName = System.IO.Path.GetFileName(postedFile.FileName); 
} 
else 
{ 
    //Mozilla/Safari 
    //TODO: this is where i need to get the mime type 
    fileName = qqfile; 
} 

每當使用比IE Request.Files其他瀏覽器包含沒有鑰匙......有沒有辦法解決這個問題? mime類型是數據庫必須的。

非常感謝!

+2

您知道客戶端MIME類型檢測不可靠,可以僞造嗎? – 2011-05-03 18:30:45

回答

1

您確定帖子變量名稱「qqfile」是否正確?檢查由jQuery調用生成的<input>標記,並驗證它是否具有name屬性的正確值。

+0

是的,螢火蟲顯示它正在正確生成。這只是IE以外的瀏覽器的問題。如果我使用Firefox,我可以將輸入流緩衝區讀入圖像(因爲MIME類型即時通訊尋找的是image/jpeg,image/tiff,imagepe/gif)。我開始懷疑這是否與IE使用multipart/form-data作爲內容類型並且firefox正在使用application/octet-stream – jsmith 2011-05-03 20:31:48

+0

嘗試在表單上設置「multipart/form-data」 'enctype'屬性。我認爲multipart需要支持文件上傳。 – marcind 2011-05-03 22:23:17

+0

我現在將帖子的內容類型作爲multipart/form-data加入firefox和IE,但Request.Files中的密鑰在firefox中仍然是空的。 – jsmith 2011-05-04 12:26:08