I am successfully uploading a file using the AjaxFileUpload control from the AjaxToolkit. I had to give IUSR write permissions to the folder but after that there was no problem.
However, the files which are uploaded are not inheriting the permissions set on the folder, so they do not have IUSR listed. On my local machine this isn't much of a problem, but on the server it means a 401 is returned as IUSR read permission is needed on the file in order to read it.
From what I can see, the folder is set to apply permissions to files inside, which leads me to believe it is something to do with the way the file is saved to the folder. Does anyone know anything more about this?
UPDATE
OK so I found some more information about the problem. This article suggested that before the file is saved to the specified location, it is first held in a temporary location. The workaround they suggest (changing the temporary location to somewhere known and setting permissions on it) does not appear to work for the AjaxFileUpload. The rest of the project temporary files appear in there, but using the Process Monitor I can see that my temporary location is somewhere else entirely for the uploaded images:
If I set the permissions on this folder for IUSR then my problem is fixed. So the next question, will this location always exist? My guess is no. If so, how can I change it?
UPDATE 2
OK so I found there was some static methods on the control which are set to build the temporary directory:
public static string BuildTempFolder(string fileId)
{
return Path.Combine(AjaxFileUpload.BuildRootTempFolder(), fileId);
}
public static string BuildRootTempFolder()
{
string path = Path.Combine(Path.GetTempPath(), "_AjaxFileUpload");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
return path;
}
One of the key lines is probably this: string path = Path.Combine(Path.GetTempPath(), "_AjaxFileUpload"); which seems to match with what I'd found in my process monitor. It seems that without rebuilding the project I'm stuck with their default settings, so I may look elsewhere for a different tool.
thanx chris
I had the same problem and I am not going to drop ajaxfileUpload control now , so I found workaround
1- download the source code from codeplex
2- open "source code directory"\Server\AjaxControlToolkit\AjaxFileUpload\AjaxFileUpload.cs and add static property name it UploadRootPath
public static string UploadRootPath
{
get;
set;
}
3- modify BuildRootTempFolder function :
public static string BuildRootTempFolder()
{
var rootTempFolder = "";
if (UploadRootPath == "")
{
rootTempFolder = Path.Combine(Path.GetTempPath(), TemporaryUploadFolderName);
}
else {
rootTempFolder = Path.Combine(UploadRootPath, TemporaryUploadFolderName);
}
if (!Directory.Exists(rootTempFolder))
Directory.CreateDirectory(rootTempFolder);
return rootTempFolder;
}
4- Build solution and use the new AjaxControlToolkit.dll in your project.
5- in your project set the directory where you want the temporary files to be saved in .
AjaxControlToolkit.AjaxFileUpload.UploadRootPath = Server.MapPath("~/Upload/Temp/");
I wish DevExpress guys do it in their next update , and I am sure they will do it in a decent way
Related
in my controller, this is the code i have
public FileResult Download(string file)
{
var vFullFileName = HostingEnvironment.MapPath("~/App_Data/Files/");
var files = uploadedfileRepository.AllIncluding();
string filename = (from f in files
select f.FileName).First();
return File(Path.Combine(vFullFileName, filename), "application/csv", filename);
}
I put the breakpoint and the file point to the right directory, but why still give me File not found exception?
and in my view this is what i have
<td>
#Html.ActionLink("Download", "Download", new { id = item.FileName})
</td
Your action method have a parameter with name file. But your are HTML will have a parameter/ query string called id, instead od file, So change your view code to
#Html.ActionLink("Download", "Download", new { file= item.FileName})
Also to get the path, try this
string fullFilePath=Path.Combine(Server.MapPath("~/App_Data"),filename)
return File(fullFilePath,"application/csv",filename);
App_Data is for database files that are accessed by MS SQL Server, and possibly your application's own data files (like a Lucene index).
IIS (and ASP.NET) are configured to block any client requests to that directory.
The solution is just to move the files to another directory. Just create a new folder in your site's root (say "CsvFiles") and link to that.
That said, why don't you serve up a HTTP 301 Redirection (or even a direct link) to the CSV files instead of serving them via your application?
UPDATE: This answer is incorrect because by serving the file contents via an MVC File response the user's browser doesn't acutally access the App_Data directory.
I want to read a *.txt file and edit this file. I use Webmatrix and it works on my computer. But after publishing it to a server (Web deploy) it doesn't work anymore.
string transmission;
string path;
path = "E:\\Documents\\My Web Sites\\Trackercontrol v2\\backup.txt";
public int insert()
{
using (StreamReader sr = File.OpenText(path))
{
while ((transmission = sr.ReadLine()) != null)
{
// etc.
}
}
}
I published this, adjusted the path to the .txt file, but the try/cath method told me that the access to the path is denied. I think reading out the file isnt the problem but editing or clearing the file makes this problem.
How can I fix this? Thank you very much!
You gave the path of your local drive, which may be or sure does not exist on the server. First upload the file on the server and then read it. You can get the correct path as
string path;
path = Server.MapPath("~/filename.txt"); // Considered file is placed at Root of your site
Check what account is the application pool running under?
Make sure that this account has read/write access to the file in concern.
Also, it would be a good idea to use
var path = Server.MapPath(relativePathToFile);
I'm trying to download and run a file to the client machine. The client is aware of that.
It's a ttkgp file that's dynamicly generated.
I've tried using Processs.Start() that worked fine on my local machine (first saved the file to C:\ then lunched it), but it's not working from the server. It's not my server but a hosted one. They are trying to help but no luck so far.
I've seen this code:
public void ProcessRequest(HttpContext context)
{
string fileName = context.Request.QueryString["filename"];
FileInfo fi = new FileInfo(fileName);
context.Response.ContentType = "application/x-rar-compressed";
context.Response.AppendHeader("Content-Disposition",
string.Format("attachment; filename=download{0}", fi.Name));
context.Response.WriteFile(fileName);
context.Response.End();
}
But since I dont know what's "HttpContext context" is, I've no idea if it works.
Is it some server previlges need to be changed? or simply this code will do the trick?
Thank you
UPDATE (24.6.12): I'm nearly finished with the problem, all I need now is to know how to open an html page in a new tab / window and close it second later. Once I'm done, I'll post back here all the process, I'm sure it'll help other people.
UPDATE (26.6.12):
Here's what I've got:
The goal is to download an TTKGP file from asp.net webiste to local user machine and run it.
Step 1: generate the file with code behaind (c#) on the server (V)
Step 2: copy the file or it's content to user machine (X)
Step 3: run the file using JS (V)
Here's the thing: I CAN copy from a text file on the server to a text file on the user machine, but not from TTKGP files. It's strange because this are just text files just a different extantion.
The code for copying text files:
enter code here
function copyremotetxt() // works
{
// copy the txt file
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newfile = fso.CopyFile("remote.txt", "C:\\Users\\***\\local.txt");
}
Perhaps I can change the file type on the user machine?
Notice 1: I know that's a security issue, the site is just for known user not the open public
Notice 2: I know there are better ways to get the task done, but there are strict limitaions on many things
Thanks for those how can help!!
This code will do the trick. It will prompt the client to download and save the file on his computer at the location he decides. What happens next with this file is the client's decision, not yours. He might simply close the Save As dialog, interrupt the download, delete the file, ... It's up to him.
Another remark: this code is extremely dangerous because it allows the client to pass any filename he wants as query string parameter and download it. So he could read absolutely all files on the server which is probably not something that you want to happen.
Ok, this need a different aproach.
I'll try using JavaScript do read the file on the server, rewrite it in the user machine and activate it. Any clues would be grate! For a start, how to I read file in JS? I'm new to it.
In my project I need store complex application settings and i dont want store it in db.
Application settings are available through administration ui to edit/change etc.
So, if i store settings in config, every time when configugration is changed, application is restart.
So second idea is loading external file from file (for example "AppSettings.conf") stored in project.
Question is pretty simple : Is possible load and save setting from external file without restarting application?
Thanks
yes, store the settings in an XML file, and you can read/write to/from an XML file just fine. But, you can't point any of the existing components stored in the web.config (such as the <authentication> or <authorization> elements) to that XML file... that won't work. Only your custom settings would.
HTH.
Finally decided to save/load application settings in db.
Rather than creating a custom config file, create a custom configuration section for the web.config:
Here's just an example:
public class SomeConfigurationSection: ConfigurationSection
{
[ConfigurationProperty("configurationData")]
public string ConfigurationData
{
get
{
return this["configurationData"] as string;
}
set
{
this["configurationData"] = value;
}
}
[ConfigurationProperty("otherConfigurationData")]
public int OtherConfigurationData
{
get
{
return Convert.ToInt32(this["otherConfigurationData"]);
}
set
{
this["otherConfigurationData"] = value;
}
}
}
EDIT
Another possible solution would be to use the Settings.setttings file under the Properties folder. I believe you can add, edit, and delete settings from here without an application restart:
//add a setting
Properties.Settings.Default.Context.Add("foo", "bar");
//edit a setting
Properties.Settings.Default.Context["foo"] = "bar";
//remove a setting
Properties.Settings.Default.Context.Remove("foo");
You can access this file under the Properties folder, or in the properties window of the web application (Properties > Settings).
This question already has answers here:
Recommended way to save uploaded files in a servlet application
(2 answers)
Closed 6 years ago.
I have a web application with an upload form where users can upload files.
I'd like to store the files a folder 'files'. And i'd like this folder to be placed directly under the webapp-root.
Using struts2, in my uploadFileAction, i can easily set this path with String uploadDir = ServletActionContext.getServletContext().getRealPath("/files")+ "/";
But when i'm trying to retrieve the files, apparently my bean which tries to access the file does not have access to getServletContext() and thus throws a nullpointer on getRealPath
Now im trying to figure out how i should approach this. I've heard about spring resource is the way to go, but i cant find any relevant examples.
Is there an easy way to get my paths? I just want the rootpath to my webapp. Nothing more nothing less...
I'd like this folder to be placed directly under the webapp-root.
This isn't going to work. All those files will get lost whenever you redeploy the webapp or even when you restart the server. Those files are namely not contained as part of the original WAR.
You need to store them in a fixed path outside the webapp's context. E.g. /var/webapp/uploads. You can configure this path in a properties file or as a system property.
I've found that a few ways to accomplish this regardless of what OS platform you're using. I typically like to store my files in some way relative to my web application. This being said, the easiest way to do this is using your class file as a reference to get the real path. I use something similar to the following to store image uploads for a few web applications that I've built:
public class FileLocationTest {
public static void main(String[] args) {
String path = FileLocationTest.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.out.println(path);
}
}
This can be done using a static utility class in your web app, or any class for that matter to obtain the real path of your application regardless of OS. Alternatively in a servlet I've also used the request class to get the Tomcat location if my appbase is in a different area, something like this:
String location = request.getClass().getProtectionDomain().getClassLoader().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
String path = location.substring(0, location.indexOf("/tomcat")) + "/data/events/images/";