locked
Uploading a specific file type only RRS feed

  • Question

  • User-2051275383 posted

    Hey again!

    I have built a file upload page using the same code as the tutorial found here: http://www.asp.net/web-pages/overview/data/working-with-files

    Is there a way to ensure that users can ONLY upload specific file types (ie. png or jpg)?

    Also I want to be able to change the file name as it's being uploaded to a different var that I have set - Is this possible?

    Friday, November 13, 2015 7:10 AM

Answers

  • User-1980594115 posted

    I used line 11, "string allowedExtensions = new[]{".png", ".jpg" };" and line 26, "if(allowedExtensions.Contains(Path.GetExtension(fileName)))" to test for extensions. If you want to change the file name, then before the "uploadedFile.SaveAs(fileSavePath)" statement (line 23), rename the fileName variable to whatever you want. Here is an example:

    @{ 
    	string fileName = "";
    	string folder = "English";
    	// Create In App_Data A Folder: CourseSubmissions
    	// Create in CourseSubmissions Folders Such As English, History.
    	var coursesRoot = Server.MapPath("~/App_Data/CourseSubmissions/");
    
    	@* Process Uploading Of Files *@
    	if (IsPost && Request.Files.Count > 0)
    	{
    		string allowedExtensions = new[]{".png", ".jpg" };
    		string fileSavePath = "";
    		var uploadedFile = Request.Files[0];
    
    		// Actual file name (name.txt). You can strip out just the 
    		// file from a path by using the Path.GetFileName method,
    		// uploadedFile saves the uploaded file.
    		fileName = Path.GetFileName(uploadedFile.FileName);
    		var extension = Path.GetExtension(fileName);
    		
    		// Get Form Variable For Folder Name
    		// Or Change The File Name To Whatever You Desire 
    		folder = Request["folderChoice"]; 
    		fileSavePath =  Path.Combine(coursesRoot, folder, fileName); 
    		
    		if(allowedExtensions.Contains(Path.GetExtension(fileName)))
    		{
    			uploadedFile.SaveAs(fileSavePath); 
    		}
    		else
    		{
    			ModelState.AddError("productImage", "Please upload only .png, .jpg & .gif files");
    		}
    	} 
    } 
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 13, 2015 10:53 AM
  • User-1980594115 posted

    Change string to var:

    var allowedExtensions = new[]{".png", ".gif", ".jpg" };



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 16, 2015 2:41 PM

All replies

  • User-1980594115 posted

    I used line 11, "string allowedExtensions = new[]{".png", ".jpg" };" and line 26, "if(allowedExtensions.Contains(Path.GetExtension(fileName)))" to test for extensions. If you want to change the file name, then before the "uploadedFile.SaveAs(fileSavePath)" statement (line 23), rename the fileName variable to whatever you want. Here is an example:

    @{ 
    	string fileName = "";
    	string folder = "English";
    	// Create In App_Data A Folder: CourseSubmissions
    	// Create in CourseSubmissions Folders Such As English, History.
    	var coursesRoot = Server.MapPath("~/App_Data/CourseSubmissions/");
    
    	@* Process Uploading Of Files *@
    	if (IsPost && Request.Files.Count > 0)
    	{
    		string allowedExtensions = new[]{".png", ".jpg" };
    		string fileSavePath = "";
    		var uploadedFile = Request.Files[0];
    
    		// Actual file name (name.txt). You can strip out just the 
    		// file from a path by using the Path.GetFileName method,
    		// uploadedFile saves the uploaded file.
    		fileName = Path.GetFileName(uploadedFile.FileName);
    		var extension = Path.GetExtension(fileName);
    		
    		// Get Form Variable For Folder Name
    		// Or Change The File Name To Whatever You Desire 
    		folder = Request["folderChoice"]; 
    		fileSavePath =  Path.Combine(coursesRoot, folder, fileName); 
    		
    		if(allowedExtensions.Contains(Path.GetExtension(fileName)))
    		{
    			uploadedFile.SaveAs(fileSavePath); 
    		}
    		else
    		{
    			ModelState.AddError("productImage", "Please upload only .png, .jpg & .gif files");
    		}
    	} 
    } 
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 13, 2015 10:53 AM
  • User-2051275383 posted

    Hey dblarie,

    Thanks for your help!

    I get an error using your code though:

    Compiler Error Message: CS0029: Cannot implicitly convert type 'string[]' to 'string'

    Source Error:

    Line 12: 	if (IsPost && Request.Files.Count > 0)
    Line 13: 	{
    Line 14: 		string allowedExtensions = new[]{".png", ".jpg" };
    Friday, November 13, 2015 5:19 PM
  • User-1980594115 posted

    Change string to var:

    var allowedExtensions = new[]{".png", ".gif", ".jpg" };



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 16, 2015 2:41 PM