I have a arrav string like this:
string[] imgList = new[] { "/Images/10000489Back20130827.jpg", "/Images/2F.jpg", "/Images/10000489Front20130827.jpg" };
that contain names of file, contained in an virtaul directory.
If this parameteres I assigned to an ImageUrl, the image is displayed. In the detail of the pages show the propertie like this:
src="/Images/1F.jpg"
But when I try to looking for the files in specific directory all the files and assigned to an ImageUrl rpoperties, the images it's not displayed. I note that the path retrieve complete, and not a reference of the virtaul directory
string path = "/Images"; ///Obtener el path virtual
DirectoryInfo directory = new DirectoryInfo(path);
FileInfo[] files = directory.GetFiles("*.jpg");
imgList = files.Where(x => x.FullName.Contains(clientNumber)).Select(x => x.FullName).ToList().ToArray();
I retrieve this path:
src="C:/Images/1F.jpg"
How can I get only the virtual path with the name of the file using DirectoryInfo class?
try this:
string path = "/Images";
DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath(path));
I resolved it:
Instead to pass the full path:
x.FullName
I concatenate the virtual path with the file name.
path + x.Name
Example
imgList = files.Where(x => x.FullName.Contains(clientNumber)).Select(x => path + x.Name).ToList().ToArray();
Show Image In Virtual Path Like (C:\Users\User\Desktop\Signature.png) : Its Working
I used Try Catch To Avoid Error
Cliend Side :
asp:Image runat="server" ID="Image1" />
Server Side :
try
{
Byte[] bytes = System.IO.File.ReadAllBytes(#"C:\Users\User\Desktop\Signature.png");
Image1.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes, 0, bytes.Length);
Image1.Visible = true;
}
catch (Exception e)
{
}
Related
My objective is User can choose an image from any place from their desktop and upload/tweet the same to twitter. I'm struggling to set the relative path to the imagePath variable.
The path (D Drive) were my image stored. While running the application it looks in different path. This throws an error System.IO.FileNotFoundException. I tried Server.MapPath too. Assist me to resolve it.
Solved the issue as following
string filePath; string imagePath = "";
if (imgFile == null)
{
imgFile = Request.Files["imgFile"];
}
if (imgFile.FileName != "")
{
filePath = Server.MapPath("~/Images/");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
filePath = filePath + Path.GetFileName(imgFile.FileName);
imgFile.SaveAs(filePath);
imagePath = Path.Combine(Server.MapPath(#"~/Images/"), filePath);
}
File must be saved in the server location, try to replace the below instruction:
string imagePath=Path.Combine(Request.MapPath("~/"),fileName);
with the below instruction
var fileName = Path.GetFileName(file.FileName);
var imagePath = Path.Combine(Request.MapPath("~/"), fileName);
file.SaveAs(imagePath);
........
Cordially
Am running the project on the visual studio 2015, When I tried to read the PDF its giving me the following error;
Access to the path 'E:\FILE\FILEUPLOAD\InnerFile\File' is denied.
Function Defination
var cd = new System.Net.Mime.ContentDisposition { FileName = "PDF.pdf", Inline = true };
string contentType = MimeMapping.GetMimeMapping("PDF.pdf");
Response.AppendHeader("Content-Disposition", cd.ToString());
var innerPath = "InnerFile/File" ;
FileInfo fi = new FileInfo(PDFUploadRootPath + innerPath + "/PDF.pdf");
byte[] bytes = System.IO.File.ReadAllBytes(PDFUploadRootPath + innerPath);
return File(bytes, contentType);
NOTE:
Given Full permission to user
Physically File Exists
I dont understand what to do now please help!
Your FileInfo instance indeed references 'E:\FILE\FILEUPLOAD\InnerFile\File\PDF.pdf':
FileInfo fi = new FileInfo(PDFUploadRootPath + innerPath + "/PDF.pdf");
but when trying to read the file contents you forgot the file name and only use the path 'E:\FILE\FILEUPLOAD\InnerFile\File':
byte[] bytes = System.IO.File.ReadAllBytes(PDFUploadRootPath + innerPath);
Thus, also add the file name for reading all file bytes:
byte[] bytes = System.IO.File.ReadAllBytes(PDFUploadRootPath + innerPath + "/PDF.pdf");
Furthermore, as others have mentioned in comments, you should really use Path.Combine to glue path parts together, not simple string concatenation...
Try using FileStream instead of byte array for reading the pdf file.
FileStream templateFileStream = File.OpenRead(filePath);
return templateFileStream;
Also check (through code) if user has write permission to directory or path:
public static bool HasUserWritePermission(String path, String NtAccountName)
{
DirectoryInfo di = new DirectoryInfo(path);
DirectorySecurity acl = di.GetAccessControl(AccessControlSections.All);
AuthorizationRuleCollection rules = acl.GetAccessRules(true, true, typeof(NTAccount));
Boolean hasPermission = false;
//Go through the rules returned from the DirectorySecurity
foreach (AuthorizationRule rule in rules)
{
//If we find one that matches the identity we are looking for
if (rule.IdentityReference.Value.Equals(NtAccountName, StringComparison.CurrentCultureIgnoreCase))
{
//Cast to a FileSystemAccessRule to check for access rights
if ((((FileSystemAccessRule)rule).FileSystemRights & FileSystemRights.WriteData) > 0)
{
hasPermission = true;
}
else
{
hasPermission = false;
}
}
}
return hasPermission;
}
I am not able to play the mp3 file in telerik rad media player.
Here is my try:
string filePath =Server.MapPath("~/App_Temp/") + "test.mp3";
if (testAudios != null)
{
byte[] bytes =Convert.FromBase64String(testAudios.FirstOrDefault().reAudio);
using (var audioFile = new FileStream(filePath, FileMode.Create))
{
audioFile.Write(bytes, 0, bytes.Length);
audioFile.Flush();
}
}
var file = new MediaPlayerAudioFile() { Title = "RESPONSE AUDIO" };
file.Sources.Add(new MediaPlayerSource()
{ Path = filePath, MimeType = "audio/mpeg" });
player.Playlist.Add(file);
this code is not working. I have checked the path, it's working. I have tried the same path with html directly. Its working successfully.
My html try is:
<telerik:RadMediaPlayer ID="player" runat="server" Width="320px" BackColor="Black"
StartVolume="80" Height="200px">
<Sources>
<telerik:MediaPlayerSource Path="~/App_Temp/test.mp3" />
</Sources>
</telerik:RadMediaPlayer>
It looks like the Path string filePath =Server.MapPath("~/App_Temp/") + "test.mp3"; you specified here isn't exactly suitable for the RadMediaPlayer in the code behind.
Try changing to a relative path should make it work for you. Something like below
var filePath = Page.ResolveUrl("~/App_Temp/test.mp3");
var file = new MediaPlayerAudioFile() { Title = "RESPONSE AUDIO" };
file.Sources.Add(new MediaPlayerSource() { Path = filePath, MimeType = "audio/mpeg", });
player.Sources.Clear();
player.AutoPlay = false;
player.Playlist.Add(file);
Hope it helps
I Want to download the files present in the Folder i.e App_Data.This file I have saved with GUID but not able to download.
My code is as follows:
public FilePathResult DownLoadDispatch(long CIDInvID)
{
var Attr = db.ConsolidatedInvoiceDispatchDocuments.FirstOrDefault(x => x.ConsolidatedInvoiceDispatchID == CIDInvID);
string path = System.Web.HttpContext.Current.Server.MapPath("~") + "/App_Data/files/Dispatch/" + Attr.StoredName.ToString();
string fileName = Attr.FileName;
return File(path + fileName, "text/plain", "test.txt");
}
I am trying to programatically build a list of files in a folder, with certain attributes like file size and modified date.
I can return the file name, but any other attribute throws an error: System.IO.IOException: The filename, directory name, or volume label syntax is incorrect.
What am I missing here?
private void BuildDocList()
{
var files = Directory.GetFiles(Server.MapPath(FilePath));
foreach (var f in files)
{
var file = new FileInfo(FilePath + f);
var fileItem = new ListItem();
// this line works fine
fileItem.Text = file.Name.Split('.')[0] + ", ";
// this line causes the runtime error
fileItem.Text = file.CreationTime.ToShortDateString();
FileList.Items.Add(fileItem);
}
}
You're trying to use the wrong filename for the FileInfo - you're using the unmapped path. You should use something like this:
string directory = Server.MapPath(FilePath);
string[] files = Directory.GetFiles(directory);
foreach (string f in files)
{
FileInfo file = new FileInfo(Path.Combine(directory, f));
// Now the properties should work.