2014-03-12 24 views
0

我正在嘗試使用WebAPI爲多個文件上載設置uploadify插件(基於「http://donnyvblog.blogspot.in/2009/05/using-jquery-plugin-uploadify-with.html」文章)。使用WebAPI設置uploadify插件,僅獲取「選擇文件」文本

這裏是我的HTML代碼:

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title></title> 
    <script type="text/javascript" src="/scripts/jquery-1.7.1.min.js"></script> 
    <script type="text/javascript" src="/uploadify/jquery.uploadify.min.js"></script> 
    <script type="text/javascript" src="/client/api scripts/products.js"></script> 
    <link href="../Uploadify/uploadify.css" type="text/css" /> 
    <script type="text/javascript"> 
    $(document).ready(function() { 
     // alert("Here"); 
     $("#fileInput").uploadify({ 
      uploader: "/uploadify/uploadify.swf", 
      script: "/api/files/Upload", 
      cancelImg: "/uploadify/cancel.png", 
      auto: true, 
      folder: "/uploads", 
      onError: function (a, b, c, d) { 
       if (d.status == 404) 
        alert("Could not find upload script. Use a path relative to: " + "<?= getcwd() ?>"); 
       else if (d.type === "HTTP") 
        alert("error " + d.type + ": " + d.status); 
       else if (d.type === "File Size") 
        alert(c.name + " " + d.type + " Limit: " + Math.round(d.sizeLimit/1024) + "KB"); 
       else 
        alert("error " + d.type + ": " + d.text); 
      } 
     }); 
    }); 
</script> 
</head> 
<body> 
     <input type="file" name="fileInput" id="fileInput" />   
</body> 
</html> 

這裏是我的控制器:

public class FilesController : ApiController 
{ 

    [HttpPost] 
    public string Upload(HttpPostedFileBase FileData) 
    { 
     /* 
     * 
     * Do something with the FileData 
     * 
     */ 
     return "Upload OK!"; 
    } 
} 

我已經建立了我的路線爲:

  config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{action}/{id}", 
      defaults: new { id = RouteParameter.Optional } 

但是,所有我得到的是「選擇文件」文本 - 沒有任何按鈕。我的控制器沒有被調用,也沒有顯示按鈕。我在這裏幹什麼?

回答

1

請勿將其用於MVC中的HttpPostedFileBase。有關詳細信息,

public class UploadController : ApiController 
{ 
    public async Task<HttpResponseMessage> PostFormData() 
    { 
     // Check if the request contains multipart/form-data. 
     if (!Request.Content.IsMimeMultipartContent()) 
     { 
      throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); 
     } 

     string root = HttpContext.Current.Server.MapPath("~/App_Data"); 
     var provider = new MultipartFormDataStreamProvider(root); 

     try 
     { 
      // Read the form data. 
      await Request.Content.ReadAsMultipartAsync(provider); 

      // This illustrates how to get the file names. 
      foreach (MultipartFileData file in provider.FileData) 
      { 
       Trace.WriteLine(file.Headers.ContentDisposition.FileName); 
       Trace.WriteLine("Server file path: " + file.LocalFileName); 
      } 
      return Request.CreateResponse(HttpStatusCode.OK); 
     } 
     catch (System.Exception e) 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); 
     } 
    } 

} 

檢查下面的文章:對於Web API,你可以不喜歡以下
http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2

+0

謝謝,除了這個uploadify PARAMATERS已在新版本也發生了變化......固定這兩個問題都解決了。 – user2645830

相關問題