在c#中使用文件路径作为字符串上传文件
Upload file using file path as string in c#
我的 C# 代码中的本地计算机文件路径仅作为字符串,我想从该路径获取文件详细信息并想将该文件上传到服务器/应用程序文件夹。
路径类似:
C:\Users\ADMIN\Desktop\Image.png
你能帮我实现这个目标吗?
文件处理对我来说都是全新的,但我在最近的 MVC 项目中就是这样做的。
ImageController:
这就是我保存文件的方式
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: 这就是我获取图像的方式:
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult ViewPost(int? id = 1)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
else
{
Post post = db.Posts.Find(id);
return View(post);
}
}
ViewPost.cshtml 这是我的看法
@model IEnumerable<BlogEngine.Models.Post>
<table class="table">
@foreach (var item in Model) {
<tr>
<td>
@*@Html.ActionLink("About this Website", "About")*@
<a href="/posts/viewpost/?id=@item.Id">@Html.DisplayFor(modelItem => item.Title)</a>
</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>
根据评论更新:
试试这个来获取文件:
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.");
}
我的 C# 代码中的本地计算机文件路径仅作为字符串,我想从该路径获取文件详细信息并想将该文件上传到服务器/应用程序文件夹。
路径类似:
C:\Users\ADMIN\Desktop\Image.png
你能帮我实现这个目标吗?
文件处理对我来说都是全新的,但我在最近的 MVC 项目中就是这样做的。
ImageController: 这就是我保存文件的方式
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: 这就是我获取图像的方式:
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult ViewPost(int? id = 1)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
else
{
Post post = db.Posts.Find(id);
return View(post);
}
}
ViewPost.cshtml 这是我的看法
@model IEnumerable<BlogEngine.Models.Post>
<table class="table">
@foreach (var item in Model) {
<tr>
<td>
@*@Html.ActionLink("About this Website", "About")*@
<a href="/posts/viewpost/?id=@item.Id">@Html.DisplayFor(modelItem => item.Title)</a>
</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>
根据评论更新:
试试这个来获取文件:
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.");
}