Remout
Remout

Reputation: 57

C# MVC How to change an uploaded pdf file name prior to further processing

I want to change the uploaded pdf's file name prior to saving on Server and saving the name/reference in my database.

In my controller I have this:

var pdf = System.Web.HttpContext.Current.Request.Files["myPDF"];

I want to change the file name, then I'll save the file and do the database work. How do I change the file name?

Upvotes: 0

Views: 1434

Answers (1)

David
David

Reputation: 218827

It technically doesn't have a file name until it's written to the file system. Anything it has in-memory is just meta-data associated with the byte stream. In the context of an HttpPostedFile those meta-data properties appear to be read-only.

Presumably at some point in your code you're saving the file. That's where you'd specify the file name:

var pdf = System.Web.HttpContext.Current.Request.Files["myPDF"];
pdf.SaveAs("anyCustomFileName.pdf");

Upvotes: 4

Related Questions