异步上传适用于服务器,手机但不适用于PC

时间:2015-06-27 19:00:00

标签: asp.net asp.net-mvc

我已经编写了一个控制器来将文件作为异步任务上传,并返回JSON,无论它是否成功。它可以在服务器和我的手机上正常工作但是当我尝试在我的计算机上运行它时会超时。我并不担心它失败,因为它可能是由于我的机器上的配置,但它甚至没有显示它失败,它只是超时。

观点:

@model Music_website.Models.MusicViewModel
@using (Html.BeginForm("Upload", "Music", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div>
        @Html.AntiForgeryToken()
        @Html.LabelFor(x => x.File)
        @Html.TextBoxFor(x => x.File, new { type = "file" })
        @Html.ValidationMessageFor(x => x.File)
    </div>
    <button class="glyphicon glyphicon-upload" type="submit"></button>
}

模特:

  public class Song
  {
      public int Id { get; set; }

      [Required]
      public string songName { get; set; }

      public byte[] File { get; set; }

      private DateTime? createdDate;
      [Required]
      [DataType(DataType.DateTime)]
      public DateTime CreatedDate
      {
          get { return createdDate ?? DateTime.UtcNow; }
          set { createdDate = value; }
      }
  }

控制器:

    // POST: /Music/Upload
    [HttpPost]
    public async Task<JsonResult> Upload(MusicViewModel model)
    {
        //var user = await manager.FindByIdAsync(User.Identity.GetUserId());
        try
        {
            foreach (var item in model.File)
            {
                Song newsong = new Song();
                byte[] uploadFile = new byte[item.InputStream.Length];
                item.InputStream.Read(uploadFile, 0, uploadFile.Length);
                newsong.songName = item.FileName;
                //newsong.Uploader = user;
                // get a stream
                newsong.File = uploadFile;
                Musicdb.Songs.Add(newsong);
                await Musicdb.SaveChangesAsync();
            }
        }
        catch (Exception)
        {
            Response.StatusCode = (int) HttpStatusCode.BadRequest;
            return Json("Upload failed");
        }
        return Json("File uploaded successfully");
    }

0 个答案:

没有答案