2014-10-03 82 views
1

有几种解决方案可以解决,但是没有一种适合我。将文件从AJAX传递到C#WebMethod

我有一个文件输入按以下:(引导文件输入)

<div class="fileinput fileinput-new" data-provides="fileinput"> 
    <div class="fileinput-new thumbnail" style="width: 200px; height: 150px;"> 
     <img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&amp;text=no+image" alt=""/> 
    </div> 
    <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 200px; max-height: 150px;"> 
    </div> 
    <div> 
     <span class="btn default btn-file"> 
     <span class="fileinput-new"> 
      Select image 
     </span> 
     <span class="fileinput-exists"> 
      Change 
     </span> 
     <input type="file" id="image" runat="server" accept=".gif|.jpeg|.jpg|.png" /> 
     </span> 
     <a href="#" class="btn default fileinput-exists" data-dismiss="fileinput"> 
     Remove 
     </a> 
     <a onclick="uploadImage();" class="btn blue fileinput-exists">Upload Image</a> 
    </div> 
</div> 

在选择的图像,并点击“上传图片”,下面的JS功能运行:

function uploadImage() { 
    var file = $("#image")[0].files[0]; 
    var name = file.name; 
    var size = file.size; 
    var type = file.type; 

    if (file.name.length < 1) { 
     return false;; 
    } 
    else if (file.size > 10000000) { 
     toastr.error('Error', 'Failed to upload image<span class=\"errorText\"><br/>Selected file is greater than 10MB.</span>'); 
     return false;; 
    } 
    else if (file.type != 'image/png' && file.type != 'image/jpg' && !file.type != 'image/gif' && file.type != 'image/jpeg') { 
     toastr.error('Error', 'Failed to upload image<span class=\"errorText\"><br/>Selected file must be in one of the following formats: .gif, .jpeg, .jpg, .png.</span>'); 
     return false; 
    } 

    var formData = new FormData(); 
    formData.append("name", name); 
    formData.append("size", size); 
    formData.append("type", type); 
    formData.append("file", file); 

    $.ajax({ 
     type: "POST", 
     url: 'Pages.aspx/UploadImage', 
     data: formData, 
     cache: false, 
     contentType: false, 
     processData: false, 
     success: function (data) { 
      data = data.d; 
      if (data == -2) { 
      toastr.error('Error', 'Failed to upload image<span class=\"errorText\"><br/>You do not have the required permissions to upload images.</span>'); 
      } else if (data == -3) { 
      toastr.error('Error', 'Failed to upload image<span class=\"errorText\"><br/>There was a fatal error. Please contact support.</span>'); 
      } else { 
      toastr.success('Success', 'Image Id <b>' + data + '</b> was saved successfully.'); 
      } 
     }, 
     error: function (err) { 
      toastr.error('Error', 'Failed to upload image<span class=\"errorText\"><br/>There was a fatal error. Please contact support.</span>'); 
     } 
    }); 
} 

关联的Web方法是目前空白:

[WebMethod] 
public static int UploadImage(object Data) 
{ 
    if (!(Security.checkAccess("/pages/Pages.aspx").Contains("w"))) // 
    { 
     return -2; 
    } 
    else 
    { 
     return -3; 
    } 
} 

Web方法最终将节省日e文件并返回ImageId或关联的错误整数。

当前,不会发生对Web方法的AJAX调用,它只是在它应该只返回两个错误消息之一时提供成功消息。

有没有什么明显的我失踪?任何帮助将不胜感激。

回答

1

我认为jquery ajax方法可能仍然不支持从您的计算机读取文件,因为直到最近对XMLHttpRequest对象进行更新使得它成为可能,因为安全性始终是javascript的情况。

的解决方案,我用使这项工作始终,而不是使用jQuery的低级别的对象或求助于老IFRAME技巧的人看到(或者更糟糕的是,闪!)

这家伙有一个非常好的关于它的教程。看到它,如果它可以帮助你:

http://blog.teamtreehouse.com/uploading-files-ajax