Steven Spielberg
Steven Spielberg

Reputation:

where i goes wrong in ASP.NET MVC 3 for uploading a file

i have a simple example of file upload who i make in ASP.NET 1 , 2 that's work in 3 with razor it's not work

upload the file using this code not work in 3 even work for 1 or MVC 2 version

 HttpPostedFileBase file = Request.Files["file"];
            if (file.ContentLength > 0) // accept the file 
            {
                string filename = Server.MapPath("~" + "/upload/" + file.FileName);
                file.SaveAs(filename);
            }
            ViewBag.filelocate = "/upload/" + file.FileName;
            return View();

are their any way to upload the file in upload folder of my application in asp.net MVC 3

Server Error in '/' Application.
--------------------------------------------------------------------------------

The given path's format is not supported. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NotSupportedException: The given path's format is not supported.

Source Error: 


Line 20:             if (file.ContentLength > 0) // accept the file 
Line 21:             {
Line 22:                 string filename = Server.MapPath("~" + "/upload/" + file.FileName);
Line 23:                 file.SaveAs(filename);
Line 24:             }


Source File: C:\Users\steven\Desktop\Web Design\File Upload\FileUpload\Controllers\HomeController.cs    Line: 22 

Upvotes: 1

Views: 2124

Answers (2)

Chandu
Chandu

Reputation: 82913

I think it is an issue with the browser sending the full file path Try this:

if (file.ContentLength > 0)
{ 
 string filename = Server.MapPath("~" + "/upload/" + System.IO.Path.GetFilename(file.FileName)); 
 file.SaveAs(filename); Line 24:             
} 

Upvotes: 2

Tomas McGuinness
Tomas McGuinness

Reputation: 7691

Are you sure you have specified the correct MIME type on the HTML form? It must be "multipart/form-data"

Upvotes: 1

Related Questions