Soy nuevo en mvc y tengo un problema. Busqué una respuesta y no pude encontrar ninguna, pero estoy seguro de que algo me saltó. El problema es que no sé cómo acceder a un archivo después de subirlo a la carpeta App_Data. Yo uso el mismo código que he encontrado en todos los foros:¿Cómo ver un archivo después de cargarlo en App_Data/Uploads en MVC 3 con Razor?
Para mi punto de vista utilizo este
@using (Html.BeginForm("Index", "Home", FormMethod.Post,
new { enctype="multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="submit" />
}
Para mi regulador del uso este
public class HomeController : Controller
{
// This action renders the form
public ActionResult Index()
{
return View();
}
// This action handles the form POST and the upload
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
// extract only the fielname
var fileName = Path.GetFileName(file.FileName);
// store the file inside ~/App_Data/uploads folder
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
// redirect back to the index action to show the form once again
return RedirectToAction("Index");
}
}
Mi modelo es
public class FileDescription
{
public int FileDescriptionId { get; set; }
public string Name { get; set; }
public string WebPath { get; set; }
public long Size { get; set; }
public DateTime DateCreated { get; set; }
}
El asunto es que quiero subir un archivo a la base de datos, y luego el WebPath para ser el enlace a mi archivo. Espero haber sido lo suficientemente claro. Cualquier ayuda sería realmente apreciada. Gracias
quiero mi base de datos para que sólo permanezca la ruta al archivo, no el propio archivo. – Robert
¿Podría definir lo que quiere decir con "ver un archivo", desea mostrarlo como un enlace o desea mostrar el contenido real del archivo? –