Upload file using file path as string in c#
我的C代码中只有本地计算机文件路径作为字符串,我希望从路径中获取文件详细信息,并希望将该文件上载到服务器/应用程序文件夹中。
路径如下:
C:\Users\ADMIN\Desktop\Image.png
你能帮我吗?我怎样才能做到?
文件处理对我来说是相当新的,但这是我在最近的MVC项目中所做的。
图像控制器:这就是我保存文件的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public ActionResult Create(FormCollection collection) { if (ModelState.IsValid) { HttpPostedFileBase file = Request.Files["userUploadedFile"]; var userName = User.Identity.Name; var selectAlbum = Request.Form["lstAlbums"]; Image img = new Image(); img.FileName = file.FileName; img.FileType = file.ContentType; img.Size = file.ContentLength; img.Path ="/uploads/"; // + userName string relativePath = img.Path + img.FileName; // relativePath.Replace("/","\"); string absolutePath = Server.MapPath(relativePath); file.SaveAs(absolutePath); img.Path = relativePath; img.User = User; db.Images.Add(img); db.SaveChanges(); return RedirectToAction("Index"); } return RedirectToAction("Create"); } |
Postcontroller:这是我获取图像的方式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
viewpost.cshtml这是我的视图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @model IEnumerable<BlogEngine.Models.Post> <table class="table"> @foreach (var item in Model) { <tr> <td> @*@Html.ActionLink("About this Website","About")*@ @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.IntroText) </td> <td> @Html.Raw(item.Body) </td> <td> @Html.DisplayFor(modelItem => item.Created) </td> <td> @Html.DisplayFor(modelItem => item.Modified) </td> <td> @Html.DisplayFor(modelItem => item.Author) </td> <td> </td> </tr> } </table> |
基于注释更新:
尝试此操作获取文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | try { Bitmap image1 = (Bitmap) Image.FromFile(@"C:\Documents and Settings" + @"All Users\Documents\My Music\music.bmp", true); TextureBrush texture = new TextureBrush(image1); texture.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile; Graphics formGraphics = this.CreateGraphics(); formGraphics.FillEllipse(texture, new RectangleF(90.0F, 110.0F, 100, 100)); formGraphics.Dispose(); } catch(System.IO.FileNotFoundException) { MessageBox.Show("There was an error opening the bitmap." + "Please check the path."); } |
来源