2013-03-06 183 views
0

我有一個問題。 我使用Kendo UI for ASP.NET MVC爲我的web應用程序創建上傳功能。它運行良好,但問題是用戶在點擊上傳按鈕執行提交表單後無法看到他們上傳的文件,並導航到其他頁面。在這種情況下,我在Create View中放置了一個上傳器,並且在將文件上傳到服務器之後,我會將該文件的鏈接存儲到db中我模型的字段中,但它始終爲空值。這裏有人能給我一些建議嗎?非常感謝。這是我使用的代碼。 *控制器:Kendo UI上傳,上傳文件後如何獲取路徑url成功

[HttpPost] 
public ActionResult Create(Model model) 
{ 
if(ModelState.IsValid) 
{ 
    model.Url = Url; 
    Db.Models.Add(model); 
    Db.SaveChanges(); 
    return RedirectToAction("Index"); 
} 
return View(model); 
} 

public ActionResult Save(IEnumerable<HttpPostedFileBase> attachments) 
     { 
      // The Name of the Upload component is "attachments" 
      foreach (var file in attachments) 
      { 
       // Some browsers send file names with full path. We only care about the file name. 
       var fileName = Path.GetFileName(file.FileName); 
       var destinationPath = Path.Combine(Server.MapPath("~/Contents/files/"), fileName); 

       DestinationPath = destinationPath; 

       file.SaveAs(destinationPath); 
      } 

      // Return an empty string to signify success 
      return Content(""); 
     } 

查看

@using (Html.BeginForm()) { 



    @Html.ValidationSummary(true) 
    <div class="editor-label"> 
      Upload file 
     </div> 
     <div style="width:45%"> 
      @Html.Kendo().Upload().Name("attachments").Async(async => async.Save("Save", "Model").AutoUpload(true)).Multiple(false) 
     </div> 
    <p> 
      <input type="submit" value="Create" class="k-button" /> 
     </p> 

型號

public string Url {get;set;} 
+0

不知道我理解你的問題。讓我解釋一下:在創建視圖中,您有一個異步Kendo上傳。用戶使用它來上傳文件。然後點擊查看保存提交表單。但保存的模型沒有指向上傳文件的鏈接。這是問題嗎? – 2013-03-06 07:32:57

+0

是的,我不知道從哪裏獲取文件的鏈接以保存到Url。 – 2013-05-03 07:25:41

回答

1

您不必返回一個空字符串。你可以返回你想要的任何字符串值。

爲了做到這一點,您需要將響應內容類型設置爲「text/plain」。

Response.ContentType = "text/plain"; 
5

客戶端代碼:

@(Html.Kendo().Upload() 
     .Name("uploadTagImage") 
     .Multiple(false) 
     .Async(async => async.Save("ApiUploadImage", "Tag").SaveField("files").AutoUpload(true)) 
     .Events(events => 
        { 
         events.Success("PV_Upload_TagImage_OnSuccess"); 
        }) 
) 

<script type="text/javascript"> 
    // Events 
    PV_Upload_TagImage_OnSuccess = function (result) { 
     var data = result.response.Data; 

     if(data.length > 0) { 
      $("#tag-image").attr('src', data[0]); 
     } 

     $.publish("PV_Grid_Access_OnAddNewRecord_Event"); 
    }; 
</script> 

服務器端代碼:

public ActionResult ApiUploadImage([DataSourceRequest] DataSourceRequest request, IEnumerable<HttpPostedFileBase> files) 
{ 
    var savedFilePaths = new List<string>(); 
    var applicationPath = System.Web.HttpContext.Current.Request.Url.Scheme + "://" + System.Web.HttpContext.Current.Request.Url.Authority + System.Web.HttpContext.Current.Request.ApplicationPath + "/Content/Images/Others/"; 
    if(files != null) 
    { 
     foreach(var file in files) 
     { 
      var fileName = Path.GetFileName(file.FileName);      
      if(fileName !=null) 
      { 
       fileName = DateTime.Now.ToString("yyyyMMddmm-") + fileName; 
       var imagePath = Path.Combine(Server.MapPath("~/Content/Images/Others/"), fileName); 
       file.SaveAs(imagePath); 
       savedFilePaths.Add(applicationPath + fileName); 
      } 
     } 
    } 

    return Json(new[] {savedFilePaths}.ToDataSourceResult(request)); 
} 
+0

謝謝。這是正確的答案。它應該被標記爲答案。 – Sven 2016-02-29 19:29:13