I'm uploading files over to a web server via a web service from my local directory. The problem I have is when I have files with the same name but from different directories. For example, I have two files:
Debug\Controls.Resources.dll
Release\Controls.Resources.dll
When the first file gets uploaded, I get a file already exists error when the second file is being uploaded.
What I want to do is include the folder information from which a file belongs to such that when it finds the file with the same name, it will create a folder.
How do I upload the files such that I can include the folder information when it gets to the server? I'm thinking of creating a data structure and upload that object instead of the file.
public class FileUpload
{
private string ContainingFolder {get;set}
private File UploadFile {get;set;}
}
PS: I've already tried zipping the files but for some reason I don't want to go that way.
Related
I am Currently working on asp.net i have worked on file upload by uploading files but i want browse a folder not a file and get all files in it and get foldername and create foldername in server and upload files it to server.Pls give me some refernce and help me to do this.
My aim is when i browse that folder all it files present in it should upload .Only files should upload not folder
Note:To browse folder not file.
Actually you are supposed to include a question if you want an answer, but it seems you are completely new to this so here are a few things you should know when it comes to asp.net and folder/file handling:
Your virtual path always corresponds to a local path on your webserver. Since it would be bad to hardcode that you might want to start off by mapping it. (e.g. /uploads/ to _C:\intepub\application\uploads)
"and get foldername"
string localpath = Server.MapPath("/uploads");
"and create foldername in server"
next you can check if that folder already exists, or if not - create it.
if(!System.IO.Directory.Exists(localpath))
System.IO.Directory.Create(localpath))
Keep in Mind though that your IIS-User needs the rights to create new directories
Now that you have made sure that your upload directory exists, you can upload/download files. Here is a good tutorial to get started:
"and upload files it to server"
http://msdn.microsoft.com/en-us/library/aa479405.aspx
"but i want browse a folder not a file and get all files in"
You can do that on the server, not the client. Your asp.net code never executes on the client.
string[] filenames = System.IO.Directory.GetFiles(localpath);
"My aim is when i browse that folder all it files present in it should upload"
That one isn't so easy, sorry. It would pose a serious security risk to users.
Maybe this will point you in the right direction (apparently it works with Chrome)
How do I use Google Chrome 11's Upload Folder feature in my own code?
When it comes to files, directories, filepaths, etc. in general, I would recommend taking a closer look at these classes:
System.IO.File
System.IO.Directory
System.IO.Path
I have write some codes for upload file in ASP.NET MVC3 project. In stead of storing file in database, I have uploaded the files in file system and stored the paths in database.
The codes for upload is as follows:-
if (file != null && file.ContentLength > 0)
{
if (path == null)
{
throw new ArgumentNullException("path cannot be null");
}
string pFileName = PrefixFName(file.FileName);
String relpath = String.Format("{0}/{1}", path, pFileName);
try
{
file.SaveAs(Server.MapPath(relpath));
return pFileName;
}
catch (HttpException e)
{
throw new ApplicationException("Cannot save uploaded file", e);
}
}
After saving the file I have used that image with image tag in several views. My codes works fine in local. But when I have hosted the site in windowsazure.com all things are working but the file upload.
How can I get rid of this situation? Please help.
One of the things you need to be aware of before trying to save the file is to ensure that the directory that you are wanting to save the file in exists. Here is a code snippet to ensure the target directory has been created on the target server.
var path= Server.MapPath("~/ImagesDirectory");
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
You may want to wrap this is a try/catch to ensure your application has the NTFS privileges to write and create directories in that location. Also, ensure that your path variable is rendering out the path that you think it should be.
Also, if the directory exists in your VS project, but does not have any content in it, then the compiler will not create that directory. To ensure the directory is created when you upload a project, insert an empty text file, such as "_doNotDelete.txt" into that directory and set it's build action to content. This will ensure the directory will be created when you do a publish.
First you should not use web application's folder beside temporary operations. Since Azure means multi-computer environment, resource (image) won't be available for requester if you use more than one instance (machine)
Let's say you uploaded on instance A's local, instance B's local won't have that file and retrieving path from DB won't change anything for instance B. You would never know which instance will give the response to request. (well, you can but it is out of the scope here) But at the end you have to realize that your upload request may go to instance A and your display request may go to instance B which will not be able to retrieve.
Anyway, the best practice is use directly blobs, their whole purpose is this. You may find more details on http://www.windowsazure.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs-20/
But if you insist on using local path and have no problem losing files (yes it will happen) use Server.MapPath("~/App_Data/...")
Till now I have been storing my uploaded files in upload folder under the website folder. But now the Drive on which the site is stored is full. How do I store new files on another drive and how should I enter the Filepath in DB?
What I do.
Have a domain (eg. domain.com) point to a website folder
Upload files in upload folder under website folder
Store file path in DB as (/upload/filename)
retrieve file with base URL (www.domain.com/upload/filename)
Now if I store files on another drive how do I enter filepath in DB which would return me the file without affecting URL request.
Create a HTTP handler. There is no reason why the URL needs to be related to any physical location or resource.
I have .torrent file and I want to extract all the file names from that file. I have tried searching but i am not having enough luck.
P.S:
I am writing a simple service which will watch a specific folder to upload my torrent files to my seedbox. Then another service will poll on my seedbox to download that downloaded files. There might be some torrents which i am not intrested to upload so my utility will upload specific torrents. I want to keep log of which torrent got uploaded so that i can fetch respective downloaded folder from my seedbox.
For reference, a torrent file may call Torrent_of_movie_2010.torrent file. But when the Torrent client would download, it will download it in a folder/or a file specified in .torrent file.
here is the simple Gist for you:
https://gist.github.com/ttrider/bde3ebf5e7af6cd2b5ee
My client has configured a SFTP server and monitoring the folder using FileWatcher. As soon s as the file are copied to SFTP server, client picks them.
If the connection is broke down while transferring the file, client picks the invalid file.
In between, I go for deleting the invalid file, client has already picked and delete that file from that folder.
How I can stop access for client to that file until I finish the full transaction.
There exist two generic options - upload the file to different folder and move it (you have denied this for your particular case) and upload the file with different name and rename the file once upload is complete.
If you control the server's architecture, you can do the following trick: upload the file with the name of filename..ext . The server will check the file name and know expected size. Once the file is of specified size, it can be picked and renamed by the server.
You should use a temporary folder for upload, and move the files in the monitored folder only when the file is completely upload.