链接ID与上载到数据库中Web api的文件

时间:2019-05-21 06:53:37

标签: c# sql asp.net

我已经成功调用了ID,但现在需要帮助,以我通过邮递员上传的文件更新数据库。当我在邮递员中键入帖子链接时,它将看起来像这样:http://localhost/PhysioWebPortal/api/PerformedExercises/PostFile/1014。 现在,我获得了id值,但是我不知道如何使用它检索和更新数据库。该图像显示了我必须更新的空FileName列。 database

 [HttpPost]
    [ResponseType(typeof(PerformedExercis))]
    //[Route("{Id}")]
    [Route("PostFile/{id}")]
    public HttpResponseMessage PostFile(int id)
    {


        //Check if Request contains any File or not
        if (HttpContext.Current.Request.Files.Count == 0)
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        var httpRequest = HttpContext.Current.Request;
        var docfiles = new List<string>();

        foreach (string files in httpRequest.Files)
        {
            var postedFile = httpRequest.Files[files];
            string filePath = HttpContext.Current.Server.MapPath("~/Uploaded/" + (DateTime.Now.ToString("MM_dd_yyyy_hh_mm_ss")) + postedFile.FileName);
            Console.WriteLine(postedFile.FileName);
            Console.WriteLine(filePath);
            var provider = new MultipartFormDataStreamProvider(filePath);
            postedFile.SaveAs(filePath);
            docfiles.Add(filePath);
        }
        //Read the File data from Request.Form collections.
        HttpPostedFile uploadedFile = HttpContext.Current.Request.Files[0];


  //Convert the File data to Byte Array which will be store in database
        byte[] bytes;
        using (BinaryReader br = new BinaryReader(uploadedFile.InputStream))
        {
            bytes = br.ReadBytes(uploadedFile.ContentLength);
        }

        //Insert the File to Database Table - FileInfo.

        PHYSIODBEntities f_Entities = new PHYSIODBEntities();



            PerformedExercis file = new PerformedExercis
            {

                FileName = Path.GetFileName(uploadedFile.FileName)

            };


             f_Entities.SaveChanges();

            f_Entities.PerformedExercises.Add(file);

            return Request.CreateResponse(HttpStatusCode.OK, new { Name = file.FileName});

    }

我需要将插入数据库代码更改为更新数据库代码

0 个答案:

没有答案