[Solved] Upload file using file path as string in c#


File-handling is all fairly new to me but, this is how I’ve done it in a recent MVC project.

ImageController:
this is how I’m saving the file

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("https://stackoverflow.com/", "\\"); 
        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: this is how I’m getting the image:

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 And this is my view

@model IEnumerable<BlogEngine.Models.Post>


<table class="table">

@foreach (var item in Model) {
    <tr>
        <td>
            @*@Html.ActionLink("About this Website", "About")*@
            <a href="https://stackoverflow.com/posts/viewpost/[email protected]">@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>

update based on comment:

Try this to get a file:

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.");
    }

source

4

solved Upload file using file path as string in c#