ASPx Dev Express File Upload Control to Mapped Network Drive - c#

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.

Related

FileUpload works well locally but not well as I publish the project to the server

I've developed a web app using asp.net c#. There is a FileUpload Control that may upload pdf or word file. But as I published the website, it is not working properly! While on the local version no problem seen. I've been trying the following:
if (FileUpload1.PostedFile != null)
{
string fn = System.IO.Path.GetFileName(empDL.client_id + FileUpload1.PostedFile.FileName);
if (FileUpload1.PostedFile.ContentLength > 0)
{
FileUpload1.PostedFile.SaveAs(Path.Combine(Server.MapPath("~/Documents/" + fn)));
empDL.uploadedfile = fn;
}
}
Maybe it is because that Documents folder hasn't write permission, you can set every one permission for this folder and try again.
Try this,
First make sure you have created your folder (although the code also checks for that) and you have permission to access that folder.
string Fileloc = "/" + "Content/Upload/YourFolder/";
if (FileUpload1.PostedFile!=null)
{
if (!System.IO.Directory.Exists(Fileloc))
{
System.IO.Directory.CreateDirectory(Fileloc);
}
var path = Path.Combine(HttpContext.Current.Server.MapPath(Fileloc), FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(path);
}
let me know if it has solved your problem or not.

Store uploaded images on Network using Ip address in 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.

Saving images relative to content directory where my site is running in IIS

I want to save images uploaded from a asp mvc page to the content/img/ folder which is relative to where my site is running in IIS. But I don't want to hard code the location on my file system to a Absolute path as this could change easily. This is the code I'm using to save them.
public static string SaveCompanyLogoImage(HttpPostedFileBase file)
{
var newFileName = GetNewCompanyLogoFileName(file.FileName);
file.FileName = newFileName;
file.SaveAs(//relative-location-here);
return Path.GetFileNameWithoutExtension(last);
}
Hope this explains my issue!
Thanks for any assistance
Use Server.MapPath("~/MyImagesFolder")
http://msdn.microsoft.com/es-es/library/system.web.httpserverutility.mappath.aspx

How to download a file from my webserver to my desktop?

I have looked around the internet for 3 hours now looking for a solution to my problem and I'm starting to wonder if anyone else has ever had this exact problem?
I am using IIS7 to host a website that does some local things around our office. Well, developing the site everything worked fine, but now that I am hosting the site I cannot (and neither can anyone else for that matter) click on the link to download the file that is needed. (lets just pretend they click on a link to download some random file from the webserver)
Well, it fails downloading the file from what I can guess as being a permissions error. I have looked for a solution but cannot seem to find one. I know very little about IIS7 so I do not understand very much about the Application Pool Identity stuff, although I did manage to grant full access for that identity to the file/folders. Anyways, here is the specific area it is messing up on..
This is a piece of the default.cshtml page that calls a Function from a .cs file:
//well.. pretty close to the exact thing, just got rid of a bunch of unecessary junk
#{
string tmp = Functions.downloadFile(fileName)
}
<html>
tmp
</html>
This is the part of the .cs file that actually downloads the file to the desktop
public static string downloadFile(string fileName) //i know this example doesnt
//use filename, but the original code does.
{
if (Directory.Exists("C:\WebSite\thingsToDownload"))
{
string[] files = Directory.GetFiles("C:\WebSite\thingsToDownload");
foreach (string s in files)
{
string[] tmp = s.Split('\\');
try
{
File.Copy(s,
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
+ "\\" + tmp[tmp.Length - 1]);
}
catch (Exception e)
{
return "ERROR COPYING: " + e.Message;
}
}
return "GOOD";
}
return "DIRECTORY DOESNT EXIST";
}
After that is all said and done, i get "ERROR COPYING: Access to the path '\fileName' is denied.
My Guess is that the webserver does not have access to the person's desktop in order to put the files there.
If anyone can shed some light and help me get this it would be greatly appreciated!
If you want to download a file by clicking a link you should use response.
Here's a sample you can use for example when a user clicks a link or button or something:
const string fName = #"C:\picture.bmp";
FileInfo fi = new FileInfo(fName);
long sz = fi.Length;
Response.ClearContent();
Response.ContentType = Path.GetExtension(fName);
Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}",System.IO.Path.GetFileName(fName)));
Response.AddHeader("Content-Length", sz.ToString("F0"));
Response.TransmitFile(fName);
Response.End();
It worked when you developed it, because it was running on your computer and thus had access to your desktop. There is no such thing as download to desktop. You can serve files and let the user decide where to save the file; on the desktop or elsewhere. But you have to do this over http, not directly from the server.

Relative, Direct URL

I have a little question about my URL .
I use a tree view on my asp page that's why I use this getcurrentdirectory .
//DirectoryInfo di = new DirectoryInfo("~" + GetTheCurrentDirectory
(selectedNodeValue));
~ = C://Inetpub//WwwRoot//
GetTheCurrentDirectory = Projects//Folder1//
So for the moment it's fine because i Can load all the files for a folder.
After I try to download the files when you click on it .
protected void Page_Load(object sender, EventArgs e)
{
string path = Request["path"].ToString();
string filename = Request["file"].ToString();
fileDownload(filename, Server.MapPath("~\\" + path + filename));
}
So I can retrieve the Path wich is the current directory . The method I use in my other page.
In the server.MapPatch should I put ~ also ? Because when I do that is works localy, but when I put this on my server , the downloading part doesn't work so I suppose this is a URL issue, I can't debug so I am really lost about this !
I changed some things :
DirectoryInfo di = new DirectoryInfo(GetTheCurrentDirectory(selectedNodeValue));
So its returns the same thing .
So now in the server.MapPath the path equal something like Projects//Folder 1//
It works locally, but still not on the server ...
Try this:
fileDownload(filename, Server.MapPath("~/" + path + filename));
And also, as a best practice, don't use + to concatenate strings. You should use string.format, so I would write the above line as follows:
fileDownload(filename, Server.MapPath(string.format("~/{0}{1}", path, filename)));
Just to help you understand your problem better, Server.MapPath will return you a physical file path on the server which corresponds to the virtual path on the web server. i.e. it converts "http://website.com/img.jpg" to something like "C:\mywebsite\img.jpg"
UPDATE:
Make sure the folder you're trying to save the file to, is not read-only and you have permissions to create files in the folder.

Categories