save chart image on file server - c#

public void SaveFileOnDisk(Chart ms, string FileName)
{
try
{
string appPath = HttpContext.Current.Request.ApplicationPath;
string physicalPath = HttpContext.Current.Request.MapPath(appPath);
string strpath = physicalPath + "\\Images";
string WorkingDirectory = strpath;
ms.SaveImage("\\\\servername\\Testing" + "\\" + FileName + System.DateTime.Today.ToString("ddmmyyyy") + ".png", ChartImageFormat.Png);
}
i want to save image on file server folder getting error Access to the path '\SERVERNAME\Testing\test09002016.png' is denied.
i have credentials for file server, but how to use them while saving image?

Related

Windows service acces denied to path 192.168.1.XX

My windows service copy a file from antoher PC in the same network.
This is the code
string fileName = "test.txt";
string sourcePath = #"\\192.168.1.XX\Users\utente\Desktop\prova\";
string targetPath = #"C:\Users\IDEA\source\repos\BotterService\bin\Debug";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.Directory.CreateDirectory(targetPath);
try
{
File.Copy(sourceFile, destFile);
}
catch (Exception ex)
{
Debug("EXCEPTION: " + ex.Message);
}
From my PC I have all permiss to read and write test.txt file. Infact I can read, write and save this file.
I got the error permission denied to 192.168.1.XX\Users\utente\Desktop\prova\
Thanks

can't access Image after uploade in asp core 2.1

I can't access photos to view after uploade.
This is the uploade code
`
public async Task<string> Upload_Image(FormFile file,string name)
{
var newFileName = string.Empty;
if (file.Length > 0)
{
var fileName = string.Empty;
string PathDB = string.Empty;
//Getting FileName
fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
//Assigning Unique Filename (Guid)
var myUniqueFileName = Convert.ToString(Guid.NewGuid());
//Getting file Extension
var FileExtension = Path.GetExtension(fileName);
// concating FileName + FileExtension
newFileName = myUniqueFileName + FileExtension;
if (string.IsNullOrWhiteSpace(_env.WebRootPath))
{
_env.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), name);
}
// Combines two strings into a path.
fileName = _env.WebRootPath + '\\' + newFileName;
// if you want to store path of folder in database
PathDB = name+"/" + newFileName;
newFileName = PathDB;
using (FileStream fs = System.IO.File.Create(fileName))
{
await file.CopyToAsync(fs);
fs.Flush();
}
}
return newFileName;
}`
I am using ID as argument for parameter 'name'.
When I am trying to access the photo using "/ID/whatEverhere.jpg"
return NOTFOUND ??????
You are storing your file in :
fileName = _env.WebRootPath + '\\' + newFileName;`
...
using (FileStream fs = System.IO.File.Create(fileName))
And you return :
PathDB = name+"/" + newFileName;
newFileName = PathDB;
...
return newFileName;
So, that isn't the same path, there isn't your ID in the path you store the file !
I sujest you should cleanup your code.

Given Paths Format is Not Supported (Visual Studio / Azure)?

When i click the download link it send me to a error page trying to debug it its telling me that my given paths format is not supported
In my controller class:
public async Task<ActionResult> DownloadBlob(string file, string extension)
{
string downloadPath = await repo.DownloadBlobAsync(file, extension);
return Json(downloadPath);
}
In My Blob Storage class:
public async Task<string> DownloadBlobAsync (string file, string fileExtension)
{
_cloudBlobContainerx = _cloudBlobClientx.GetContainerReference(containerNamex);
CloudBlockBlob blockBlob = _cloudBlobContainerx.GetBlockBlobReference(file + "." + fileExtension);
var path = downloadPath + file + "." + fileExtension;
using (var fileStream = System.IO.File.OpenWrite(path))
{
fileStream.Position = 1;
//fileStream.Seek(0, SeekOrigin.Begin);
await blockBlob.DownloadToStreamAsync(fileStream);
return path;
}
}
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: The given path's format is not supported
The source of the error :
using (var fileStream = System.IO.File.OpenWrite(path))
According to your description, I suggest you could set a break point to check the value of downloadPath or 'path' field.
I have created a simple demo to download a blob file to a local folder path. It works fine. You could refer to.
string containerNamex = "container002";
string file = "MyTest"; //a blob file named MyTest.txt
string fileExtension = "txt";
string downloadPath = #"D:\MyBlob\";
CloudBlobClient _cloudBlobClientx = storageAccount.CreateCloudBlobClient();
_cloudBlobContainerx = _cloudBlobClientx.GetContainerReference(containerNamex);
CloudBlockBlob blockBlob = _cloudBlobContainerx.GetBlockBlobReference(file + "." + fileExtension);
var path = downloadPath + file + "." + fileExtension;
using (var fileStream = System.IO.File.OpenWrite(path))
{
fileStream.Position = 1;
blockBlob.DownloadToStreamAsync(fileStream);
Console.WriteLine("success");
}
The result like this:

How to Copy URI path file into temp folder?

I have a file path in URI and trying to copy the URI file into C:\temp\ but I am getting an error "Could not find file (from the URI path)" If anyone suggest me would be a great helpful. Thank you.
public String getFile(String uri)
{
// Download file in temp folder
String fileName = Path.GetFileName(uri.Replace("/", "\\"));
Uri fileUri = new Uri(uri);
string fullFilePath = absoluteUri.AbsoluteUri.ToString();
string localPath = new Uri(fullFilePath).LocalPath;
String tempFolder = #"C:\temp\";
File.Copy(localPath, tempFolder);
return fileName;
}
You can use web client to download a file from a Uri
new WebClient().DownloadFile(uri, Path.Combine(filePath, fileName));
I tried in a different way and its working for me. I hope this could help for someone else.
private String getFile(String uri)
{
Uri uriFile = new Uri(uri);
String fileName = Path.GetFileName(uri);
List<String> fileData = new List<String>();
// Reads all the code lines of a file
fileData = readCodeLines(uriFile);
String tempPath = #"c:\temp\";
try
{
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
File.WriteAllLines(tempPath, fileData);
}
catch (IOException ex)
{
MessageBox.Show("Could not find the Temp folder" + " " + tempPath);
}
return fileName;
}

How to upload a file and save it in an specific folder in local computer?

I want to take (upload) a file to a specific folder that I have created in my project (on local computer not a server!).
Here is the code that I am using:
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("images/" + filename));
I have added the Fileupload, and the code above in a button. But the code won't work. What's the problem?
I also used this form:
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("~DesktopModules/UshtrimiDyte/images/" + filename));
I also used it with double back slashes, but that didn't work either. How can I solve my problem?
Try the above
string path = Server.MapPath("~DesktopModules/UshtrimiDyte/images/" + filename);
System.IO.Stream myStream;
Int32 fileLen;
byte[] Input = null;
fileLen = FileUpload1.PostedFile.ContentLength;
if (fileLen > 0)
{
Input = new Byte[fileLen];
myStream = FileUpload1.FileContent;
myStream.Read(Input, 0, fileLen);
string I_Docx_type = FileUploadFieldDoc.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
WriteToFile(path + "." +I_Docx_type, ref Input);
path += "." + I_Docx_type;
}
method
private void WriteToFile(string strPath, ref byte[] Buffer)
{
// Create a file
System.IO.FileStream newFile = new System.IO.FileStream(strPath, System.IO.FileMode.Create);
// Write data to the file
newFile.Write(Buffer, 0, Buffer.Length);
// Close file
newFile.Close();
}

Categories

Resources