Click here to Skip to main content
15,946,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! i WANT to remove special character from filename while uploading file in C#, asp.net.

What I have tried:

ASP.NET
if (UploadFile.HasFile)
        {
           
            hdOrgFileName.Value = UploadFile.FileName.Substring(0, UploadFile.FileName.LastIndexOf("."));
            hdFileName.Value = UploadFile.FileName.Substring(0,UploadFile.FileName.LastIndexOf(".")) + DateTime.Now.ToString("MMmm");
            UploadFile.SaveAs(Server.MapPath("PdfTemplate/" + hdFileName.Value + ".pdf"));
            SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
            f.Serial = "10025522771";
            f.HtmlOptions.IncludeImageInHtml = true;
            f.OpenPdf(Server.MapPath("PdfTemplate/" + hdFileName.Value + ".pdf"));

            if (f.PageCount > 0)
            {
                int result = f.ToHtml(Server.MapPath("HtmlTemp/" + hdFileName.Value + ".html"));
            }

            dvPDF.Attributes.Add("w3-include-html", "HtmlTemp/" + hdFileName.Value + ".html");


            RunJs("hidepopup();");

        }
Posted
Updated 3-Feb-21 20:46pm
v2

First decide what is a "special character" and exactly what you are going to do with it.
In many cases, the simplest solution is to specify a range of "wanted" characters and replace or remove everything else.

The simplest way to do that is to use a Regex.
For example, to replace anything which isn't a letter or digit with '_' is pretty trivial:
C#
string newName = Regex.Replace(fileName, "[^\\w]", "_");
 
Share this answer
 
Comments
Member 14976405 4-Feb-21 2:48am    
is this i add to my c# code?
OriginalGriff 4-Feb-21 3:34am    
If that's what you want to do - replace anything that isn't a alphanumeric character with "_" - then yes. What I'd do is grab a set of "sample inputs" and try the code in a small test program to make sure it does exactly what you want. If it doesn't, it's easier to tweak it there than in your main app.
Member 14976405 4-Feb-21 4:57am    
i am confused how to use this in my code!
OriginalGriff 4-Feb-21 5:24am    
How are you confused?
You have a method call, that takes an input string and returns a modified string.

What do you expect to do with it?
Member 14976405 4-Feb-21 7:51am    
Thank you its working now!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900