Store uploaded images on Network using Ip address in c# - c#

I am working on uploading a file from MVC and I want to store the uploaded images on the network or any other system,I have IP address of that system,
Can anyone help me to find out where I am making mistake or is there some other way to store image on certain IP address.
I am using this code to save image on another machine with IP address
public ActionResult Test(string ENROLLIMAGE)
{
if (!string.IsNullOrEmpty(ENROLLIMAGE))
{
HttpPostedFileBase file = Request.Files["ENROLLIMAGE"];
Guid ImageId = Guid.NewGuid();
var filename = ImageId.ToString() + Path.GetExtension(file.FileName);
file.SaveAs(Server.MapPath("\\\\192.168.11.113\\D:\\UploadedFiles" + filename));
Uri addy = new Uri("\\\\192.168.11.113\\D:\\UploadedFiles" + filename);
}
return View();
}
Kindly help !!!!
It is giving me error while I am trying to upload that "The given path's format is not supported."
Thanks

You can't use a drive specification like D: in an UNC path. What you should do is make a network share of the UploadedFiles folder on the remote machine, and use file.SaveAs("\\\\192.168.11.113\\UploadedFiles\\" + filename);
D: is not allowed in an UNC path, and will give you the error.
So, either use file.SaveAs("\\\\192.168.11.113\\<ShareNameHere>\\" + filename), or use file.SaveAs("\\\\192.168.11.113\\D$\\UploadedFiles\\" + filename) if you have administrative privileges on the target machine.

Related

ReadAllBytesAsync(path) path always combined with the contentrootpath

I want to return a files from virtual path directory, currently this is my code in C# asp net core.
var path = GetConfig.AppSetting["VirtualDirectoryPath:PathLocal"] + "/" +
_.data.Downloadpath;
var provider = new FileExtensionContentTypeProvider();
if (!provider.TryGetContentType(path, out var contentType))
{
contentType = "application/octet-stream";
}
var bytes = await System.IO.File.ReadAllBytesAsync("http://website.com/foldername/foldername/filename.pdf");
return File(bytes, contentType, Path.GetFileName(path));
the DownloadPath contains 'foldername/filename.pdf', and the virtual directory path contains the localhost domain, i tried to hardcode the path but it keeps returning "D://folder/folder/http://website.com/foldername/foldername/filename.pdf". I don't know why the url combined with the contentrootpath, can someone help me?
Actually, i also want to retrieve a virtual directory path as url from the virtual directory that i already created in the iis so that i can download the files from the url. I don't know how to code better, if you can help me with this too i'd very much appreciate it. TIA.
Ultimately, the problem is that you seem to believe File.ReadAllBytesAsync downloads files from the web, it does not. It only reads files on your local file system.
And because http:// files aren't supported, they're not considered to be rooted, and so it gets concatenated to the "current folder" and you get what you see.
Also never concatenate paths like that, that's what Path.Combine is for.

Image is not shown on live server

I have a logo that I need to show inside a mail. On my local machine, the logo is shown, but on the server where the live environment is, the logo is not shown.
I'm using the path like this:
imagePath = "~/Images/email-logo2.png";
The email-logo2.png exists in the same folder on the local machine and on the server, too.
I have tried to add permissions to read in the server folder where the png exists, but it did not resolve the problem. Can you advise?
The image is added in the email like this:
HTML:
<img class="auto-style4" src="{PictureSrc}" /><br />
C# code:
switch (property)
{
case "PictureSrc":
string imagePath = "";
if (User.Identity.GetUserId<int>() == 3140 ||
User.Identity.GetUserId<int>() == 3142)
{
imagePath = "~/Images/email-logo2.png";
}
else
{
imagePath = "~/Images/email-logo.png";
}
content = content.Replace("{" + property + "}", HttpContext.Server.MapPath(imagePath));
Server.MapPath returns a path on disk (e.g. C:\images\image.png), not a URL. See https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx.
So a user viewing the email from another machine will obviously not be able to resolve a path on the server's disk, it has no access to that disk.
The image path you provide has to be a fully qualified URL e.g. http://www.example.com/images/image.png.
It worked locally because you're on the same machine as where the image is located, so it happens to have access to that path, but that's not true for everyone else using it.
Alternatively, if the image is not too large you can convert it to base64 and embed it into the HTML in the email.

Saving images on the server

I'm getting the "generic error occurred on GDI++ wen trying to save images.
What I am missing here?
string appdatapath = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
if (!Directory.Exists(appdatapath + "/Images")
Directory.CreateDirectory(appdatapath + "/Images", Directory.GetAccessControl(appdatapath));
if (!Directory.Exists(appdatapath + "/Images/XBLContent/"))
Directory.CreateDirectory(appdatapath + "/Images/XBLContent/", Directory.GetAccessControl(appdatapath));
string imagesdir = Path.Combine(appdatapath, "/Images/XBLContent/");
Image image = FeedUtils.RequestImage(String.Format({0}/{1}/image.jpg", url, c.GUID));
image.Save(imagesdir + c.GUID + "sm.jpg");
Check whether your IIS worker process has write permissions to the local file system, and in particular the folder whether you are saving the image.
Also, use Server.MapPath() to get the physical location.
See this blog for a solution.

ASPx Dev Express File Upload Control to Mapped Network Drive

Here is my function, and as you can see I have the upload going into the web sites directory/files ... I am hosting the site on IIS with another site & need the files to upload to the mapped network drive DOCSD9F1/TECHDOCS/
No idea what the folder path should be...
any help would be greatly appreciated
protected void ASPxUploadControl1_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
{
if (e.IsValid)
{
string uploadFolder = Server.MapPath("~/files/");
//string uploadFolder = "//DOCSD9F1/TECHDOCS/";
string fileName = e.UploadedFile.FileName;
e.UploadedFile.SaveAs(uploadFolder + fileName);
e.CallbackData = fileName;
}
}
Use backslashes instead of slashes for the network path. If it doesn't work, make sure the ASP.Net account has adequate permissions to write to the share.

File.Exists using the wrong root path?

In my c# class I wrote I have a photo property that returns the photo source if the image exists (nothing or default image otherwise). In my code I use:
public string Photo
{
get
{
string source = "~/images/recipes/" + id + ".jpg";
if (File.Exists(source))
return "~/images/recipes/" + id + ".jpg";
else
return "";
}
}
If I get the FileInfo() information for this image I see that I tries to find this image in the following directory: C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\~\images\recipes
Of course the image is not located in that directory and File.Exists is returning me the wrong value. But how can I fix this?
Try this:
if(File.Exists(System.Web.HttpContext.Current.Server.MapPath(source)))
You need to map the relative path back to a physical path:
string source = HttpContext.Current.Server.MapPath("~/images/recipes/" + id + ".jpg");
You'll have to use:
Server.MapPath(source)
As you can not be 100% sure where the code will be running from, ie. it will be different in development and on a production server. Also are you sure ~/ works in windows? Wont that just be interpreted as a directory named ~? Unless thats what you want.

Categories