How image uploading file name replace for CKFinder with asp.net? - asp.net

I'm using file browser with CKFinder. But I have a problem.
During uploading filename is not saving Turkish format.
I want to load the file by changing the name.
Can any Replace method be applied?
For example;
File name: türkçe karakter sıkıntısı.jpg,
Modified name: turkce-karakter-sikintisi.jpg
Thank you.
Good luck with.

Read CKFinder. Rename uploaded files.
i.e. you need to update FileUploadCommandHandler.cs where you can modify sFileName in a way you like. To remove accent characters you can use code from How do I remove diacritics (accents) from a string in .NET?, to replace spaces you can use simple replace()
Example:
sFileName = System.IO.Path.GetFileName( oFile.FileName );
if ( Connector.CheckFileName( sFileName ) && !Config.Current.CheckIsHiddenFile( sFileName ) )
{
//custom logic
sFileName = RemoveDiacritics(sFileName).Replace(" ", "-");
//other logic
// Replace dots in the name with underscores (only one dot can be there... security issue).
...

Related

Save text file with code page UTF-8 in Axapta 3.0

How do I make a text file with code page UTF-8 in Axapta 3.0?
I cannot use
myFile = new CommaIo(myFileName, 'W', 65001);
as we can in newer versions of Axapta. In Axapta 3.0 new CommaIo only have the first two parameters.
I've not worked in 3.0, but I have a few ideas for you that might send you the right way.
1) Do you have CommaTextIo or TextIo? Those are the objects where you can specify a code page.
2) Look in the AOT and see if you have a macro called #File, and inside if you have #utf8Format(65001), use the X-Ref (or Ctrl+F) to find other places in the system that use it. Then you can see how they might accomplish UTF-8
3) See if you can combine CommaIo with some .NET code, or just manually generated a CSV. Perhaps generate your CSV and write it, then read it and re-write it using a method like below (from MetadataXMLGenerator job):
void write(str _directory, str _name, str _text)
{
str path;
;
_text = System.Text.RegularExpressions.Regex::Replace(_text, '\n', '\r\n');
if (!System.IO.Directory::Exists(_directory))
{
System.IO.Directory::CreateDirectory(_directory);
}
path = System.IO.Path::Combine(_directory, _name);
System.IO.File::WriteAllText(path, _text, System.Text.Encoding::get_UTF8());
}

PHPExcel different behavior on different systems

I have PHPExcel on localhost and on server.
When I try to read xlsx file on localhost - all ok, but when I try to read same file on server - all cells with cyrillic words are empty.
All systems have same PHPExcel and PHP versions.
What could be the problem?
Thanks!
The problem with this file is that it was created by an application that doesn't recognise case-sensitivity in filenames.
The rels table indicates that the Shared Strings table (where all the text string values for the workbook are stored) is called sharedStrings.xml, but the actual file in the zip is called SharedStrings.xml. A file generated by MS Excel itself uses the correct case in the filename, so I'm guessing that this file was created using some third-party tool or library. MS Excel is clearly more forgiving about case-sensitivity in filenames, allowing it to read the zip regardless.
I can probably fix this by using
$zip->getFromIndex(
$zip->locateName('sharedStrings.xml', ZIPARCHIVE::FL_NOCASE);
);
rather than
$zip->getFromName('sharedStrings.xml');
but it will take me a couple of days to implement the fix
EDIT
Somewhere around line 310 of the /PHPExcel/Reader/Excel2007.php file is the getFromZipArchive() method that can be changed to read:
private function getFromZipArchive($archive, $fileName = '')
{
// Root-relative paths
if (strpos($fileName, '//') !== false) {
$fileName = substr($fileName, strpos($fileName, '//') + 1);
}
$fileName = PHPExcel_Shared_File::realpath($fileName);
// Apache POI fixes
$contents = $archive->getFromIndex(
$archive->locateName($fileName, ZIPARCHIVE::FL_NOCASE)
);
if ($contents === false) {
$contents = $archive->getFromIndex(
$archive->locateName(substr($fileName, 1), ZIPARCHIVE::FL_NOCASE)
);
}
return $contents;
}
and will then be able to access the Shared String file case-insensitively

Deleting file after upload

I am trying to upload a file with the FileUpload control. When file is uploaded, I extract information from it and then i want to delete it.
I manage to upload it, save it and get the info from it, but when i try to delete it i get the follwing exception
"The process cannot access the file 'D:\IIS**OMITTED***\V75 personal ny.csv' because it is being used by another process.
string fn = Path.GetFileName(fu.PostedFile.FileName);
string SaveLocation = Server.MapPath("UploadedCSVFiles") + "\\" + fn;
FileInfo fi = new FileInfo(SaveLocation);
fu.PostedFile.SaveAs(SaveLocation);
fu.PostedFile.InputStream.Dispose();
DataTable dt = AMethodThatUsesFile(SaveLocation);
fi.Delete();
Try this code to delete file.
System.IO.File.Delete(SaveLocation );
You specified a method AMethodThatUsesFile(SaveLocation);. If it uses any classes like StreamReader to read file, please close the reader using StreamReader.Close(); method before trying to delete
dispose the fi before deleting. and then us File.Delete(). remember to use using statements when use disposable objects, or dispose it after use.
using System.io
File.Delete(Server.MapPath("../Nurturing/" + fnevents));
FileInfo fInfoEvent;
fInfoEvent = new FileInfo(fnevents);
fInfoEvent.Delete();
here fnevents is the name of the file that u are deleting. Nurturing is the name of the folder.

file upload control problem

i am using file upload control in server side when iam trying to get the file it is showing no file present
<asp:FileUpload ID="upldDocument" runat="server" />
string fileExtension = System.IO.Path.GetExtension(upldDocument.FileName);
if (upldDocument.HasFile)
{
}
i am getting a empty string as file extension and upldDocument.HasFile is returning false even after selecting a file.
what could be the reason??
Based on the posted code, I can only offer a best guess. There's not enough code posted to be sure what the problem is, but here's my best guess:
If you're not already, you need to check the HasFile property.
See here for a full example:
Edit - added
Using HasFile AFTER the bad code won't help. You need to put the code to get the extention inside an if statement so that it only attempts to read the extension if there IS a file.
string fileExtension = "";
if (upldDocument.HasFile)
{
fileExtension = System.IO.Path.GetExtension(upldDocument.FileName);
}
else
{
//No file selected by user, which is why you can't get the extension.
// handle this eventuality here even if it means just returning from the function and not doing anything.
}
How are you checking the values? (in what event)
Did you set the enctype attribute of the form to "multipart/form-data" ?

Add filter to FileUpload Control

How to add filter to the fileupload control in asp.net? I want a filter for Word Template File (.dot).
You could also do a javascript alternative to filtering it server side (you'd probably want to do that as well) but this saves the client from spending the time waiting on an upload to finish just to find out it was the wrong type.
http://javascript.internet.com/forms/upload-filter.html
So basically you just run a javascript function on submit that parses off the extension of the uploaded file and gives them an alert if its not of the right type.
You could also use document.forms[0].submit(); instead of passing the form reference through (as ASP.NET really only uses a single form (unless your doing something funky))
string fileName = fuFiles.FileName;
if(fileName.Contains(".dot"))
{
fuFiles.SaveAs(Server.MapPath("~/Files/" + fileName));
}
If you mean to filter the file extensions client/side, with the standard browser's file selector, isn't possible.
To do that you have to use a mixed type of upload, such as SWFUpload, based on a flash uploader system (that's a really nice techinque: it allows you to post more than a file at time).
The only thing you can do in standard mode is to filter the already posted file, and I suggest to use System.IO.Path namespace utility:
if (Path.GetExtension(upFile.FileName).ToUpper().CompareTo(".DOT") == 0)
{
/* do what you want with file here */
}
Check the filename of the uploaded file serverside:
FileUpload1.PostedFile.FileName
Unless you want to use java or something similar on the client, there's really not much you can do for filtering uploaded files before they're sent to the server.
Here I have a small method that I used to filter which types of files can be uploaded by the fileupload control named fuLogo.
if (fuLogo.HasFile)
{
int counter = 0;
string[] fileBreak = fuLogo.FileName.Split(new char[] { '.' });
logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString()+ "." + fileBreak[1]);
if (fileBreak[1].ToUpper() == "GIF" || fileBreak[1].ToUpper() == "PNG")
{
while (System.IO.File.Exists(logo))
{
counter++;
logo = Server.MapPath("../Images/Logos/" + fileBreak[0] + counter.ToString() + "." + fileBreak[1]);
}
}
else
{
cvValidation.ErrorMessage = "This site does not support any other image format than .Png or .Gif . Please save your image in one of these file formats then try again.";
cvValidation.IsValid = false;
}
fuLogo.SaveAs(logo);
}
basically, I first Iterates through the directory to see if a file already exists. Should the file exist, (example picture0.gif) , it will increase the counter (to picture1.gif). It prevents that different users will overwrite each other's pictures should their pictures have the same name.

Resources