Save an image to outside the directory in asp.net - asp.net

I am trying to save an image to server.Server directory structure is as follows
httpdocs-
Folder-
Page.aspx
and
httpdocs-
Images-
Subimages
The Folder and Images are under the httpdocs. I need to save the image to subimages folder from pages.aspx. Saving image code is on pages.aspx.
I tried
string CroppedImagePath = Server.MapPath("~/Images/Subimages"+file)
But not get the exact result

Unless file contains a leading / character, that's going to end up saving as something like:
~/Images/Subimagesfilename.ext
Use Path.Combine() to build a path name. Something like this:
var CroppedImagePath = Path.Combine(Server.MapPath("~/Images/Subimages"), file);

try this
string CroppedImagePath = Server.MapPath("~/Images/Subimages/"+file)
/ after Subimages

Related

How to get rid of the original file path instead of name in PGP encryption with ChoPGP library?

I tried PGP encrypting a file in ChoPGP library. At the end of the process it shows embedded file name along with the whole original file name path.
But I thought it will show only the filename without the whole path. Which I intend to work on and figure out a way to do so?
Doing the following:
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
pgp.EncryptFile(#"\\appdevtest\c$\appstest\Transporter\test\Test.txt",
#"\\appdevtest\c$\appstest\Transporter\test\OSCTestFile_ChoPGP.gpg",
#"\\appdevtest\c$\appstest\Transporter\pgpKey\PublicKey\atorres\atorres_publicKey.asc",
true,
true);
}
which will result in:
But I would like to only extract the Test.txt in the end something
like this:
Looking at this line from the ChoPGP sources: https://github.com/Cinchoo/ChoPGP/blob/7152c7385022823013324408e84cb7e25d33c3e7/ChoPGP/ChoPGPEncryptDecrypt.cs#L221
You may find out that it uses internal function GetFileName, which ends up with this for the FileStream: return ((FileStream)stream).Name;.
And this one, according to documentation, Gets the absolute path of the file opened in the FileStream..
So you should either make fork of ChoPGP and modify this line to extract just filename, or submit a pull request to the ChoPGP. Btw, it's just a wrapper around the BouncyCastle so you may use that instead.

ASP File Object Issue

I am working on an ASP classic website, which the client reported suddenly exhibited an issue with a previously working function which listed only "image" file types. Upon reading the code, I found that the loop that lists the files in a folder, uses the InStr() function to identify files by their type, which should be "image." However, I found that something must have changed in the OS, as the type is not longer "image", but "JPG", or "PNG", etc. This dramatically changes the way the code works. Following is a snipped of the code:
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Server.MapPath(sCurrentDirectoryPath))
Set oSubFolder = oFolder.Files
iFileCount = 0
For Each oFileName in oSubFolder
If InStr(1, LCase(oFileName.Type),"image") > 0 Then
iFileCount = iFileCount + 1
End If
Next
Because the InStr() function is trying to find a file type of "image", no files are counted up, and the function returns zero files found. Whilst debugging, I found that the value being returned by oFileName.Type, was as follows:
This is the file type:JPG File
This is the file type:JPG File
This is the file type:Text Document
This is the file type:Data Base File
Files in the folder were two "whatever.jpg" files, a "whatever.txt" file, and a "thumbs.db" file. So, it appears that the OS (Windows Server 2019) may have changed to be less generic with reporting an "image" file, and is now reporting "JPG file" or "PNG file", etc. This of course, breaks this code! Are there any suggestions from you'all on how I could go about modifying this code to work on reporting exactly how many image files are present?
On Windows 10, the Type values for .jpg and .png files are JPEG image and PNG image respectively. What OS are you using?
Also, Type doesn't actually analyze the file, you could have a virus.exe file in the folder that has been renamed to virus.jpg, and the Type value will still show it as JPEG. So if the function is indented to check user uploaded content to ensure images are actually images, the Type value will be of no use. If you have root access you could install a COM DLL that uses a program such as ExifTool to properly analyze files (https://github.com/as08/ClassicASP.ExifTool), however that would be a complete rewrite.
But assuming you're not looking to check if an image file is actually an image file, you could just split the filename extensions and use Select Case to count the image files if your OS is returning just XXX file and no longer XXX image in the Type value (alternatively you could split the Type value, but you'd still need to check for valid image file extensions):
Dim oFSO, oFolder, oSubFolder, oFileName, iFileCount, oFileNameSplit
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(Server.MapPath(sCurrentDirectoryPath))
Set oSubFolder = oFolder.Files
iFileCount = 0
For Each oFileName In oSubFolder
oFileNameSplit = Split(oFileName.Name,".")
If uBound(oFileNameSplit) > 0 Then
Select Case Trim(lCase(oFileNameSplit(uBound(oFileNameSplit))))
Case "jpg", "jpeg", "png", "gif" ' Maybe add some more extensions...
iFileCount = iFileCount + 1
End Select
End If
Next
Set oFSO = Nothing
Set oFolder = Nothing
Set oSubFolder = Nothing
Response.Write(iFileCount)

Qt get File directory based on directory content

I have few directorates and one file *.txt under one Directory A, Directory path is in QString
say c:/A/1/2/3/4
c:B/C/A/1/2/3/4/5/6
In my code I have only the full path, now I want to get the directory until A(name of A can change anytime) based on the file *.txt
inshort:- I want to parse all directory and get the directory until the place where *.txt present from right to left
Are you trying to get the string of the file path to A? Ex. If the file path was:
C:/Users/Admin/Desktop/file.txt
you want to get:
C:/Users
If so, all you have to do is find the second index location of "/". You could do something like this:
QString filePath = "C:/Users/Admin/Desktop/file.txt";
int index = filePath.indexOf("/");
index = filePath.indexOf("/",index+1);
QString shortenedPath = filePath.mid(0,index);
This should give you:
C:/Users/
If that isn't what you meant, then sorry for the long response.

How to find a file path in current java directory

I have a function that gets a file path as an input. input file is located in a folder in my project (etc/xsd/template.xsd). how can i set this path?
this ia my function:
JAXBUtilityTool tool = new JAXBUtilityTool("etc/xsd/template.xsd","src.com.classes");
and it can not find the file "etc/xsd/template.xsd"
System.getProperty("user.dir") does not help since when I add the rest of path to it:
System.getProperty("user.dir")+ "etc/xsd/template.xsd"
result is c:\eclipse\myworkingdirectory\project/etc/xsd/template.xsd
Try:
String path = System.getProperty("user.dir");
path = path.replaceAll("\\","/") + "/etc/xsd/template.xsd"
Source:
String.replaceAll single backslashes with double backslashes

Add file at root of zip file using DotNetZip with classic ASP

I have DotNetZip installed and running fine on a Windows 2008 server.
Using a classic ASP page, I want to bundle a bunch of comma-delimited files to a user and send it over in a zip file.
The following code works fine but it stores all the path information so the files inside the zip file are located in some ridiculous directory like C:\Inetpub\wwwroot\appname\_temp\
I'm using the following code:
Set objZip = CreateObject("Ionic.Zip.ZipFile")
sFileArray = Split(sFileArray, "|")
For iCount = 0 To UBound(sFileArray)
If sFileArray(iCount) <> "" Then
objZip.AddFile sFileArray(iCount)
End If
Next
objZip.Name = sFilePath & "test.zip"
objZip.Save()
objZip.Dispose()
Set objZip = Nothing
I see that the AddFile method allows you to specify where you want the added file to reside in the zip file if you add a second parameter. According to the documentation objZip.AddFile sFileArray(iCount), "" should put the file in the root of the zip file.
However, when I add that parameter, I get the following error:
Wrong number of arguments or invalid property assignment: 'objZip.AddFile'
Anyone have any idea what I'm doing wrong?
Thanks.
I think you are misinterperting the documentation. If the second parameter is null then the directory path of the file being added is used. If the second parameter is an empty string "" then the file is added to the root level in the zip. A quick look into the Ioniz.zip.dll shows that the single parameter override of AddFile method simply calls the the double parameter override with the second parameter set to null.
Hence your add file should look like:
objZip.AddFile sFileArray(iCount), ""
to get the result you are after.

Resources