简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM