how to save a excel file in a particular path? - asp.net

Response.AddHeader("content-disposition", "attachment;filename=abc.xls");
Response.ContentType = "application/excel";
I am using the code above to download an Excel file but I want to download the Excel file to a specific path.
How can I do this?

It is impossible. Server can't to locate client machine for particular destanation of downloading.
RFC 2183:
The receiving MUA SHOULD NOT respect any directory path information
that may seem to be present in the filename parameter. The filename
should be treated as a terminal component only. Portable
specification of directory paths might possibly be done in the future
via a separate Content-Disposition parameter, but no provision is made
for it in this draft.

It can't be done by server side it's your os and browser settins which tells where the downloaded file should be placed as Alexgender says
"The filename should be treated as terminal component only"

Related

Get file path & filename with asp:FileUpload, don't want file... just path and name

I am developing a .NET intranet site which will enable the user to see a list of files (file details stored in DB) and link to the actual PDF/XML/XLS and open it... kind of like a table of contents for the network.
During data entry, the user enters various data about a document, then browses to the file on the network and selects it using the asp:FileUpload. The codebehind then saves the network path to the DB. There is alot of overhead here because i'm sending the file to the server but never use it.
Everything has been working fine until someone tries to use a large PDF file then I get the dreaded MAXIMUM REQUEST LENGTH EXCEEDED error... So I'm trying to find a solution here... I do not need the actual file.. just the path and filename.
I know not all browsers send the full path but our systems have older browsers so everything is working fine now, but will probably break soon.. which is another reason to find a different solution.
I've looked into Javascript to pull the path but that won't work...
Any other ideas? Other ways to just grab the path and filename? (besides manually typing it in to a Text field)
Thanks,
Todd.
This may help too
How to get the full path of a file from asp: file upload?
string filename = Path.GetFileName(FileUpload1.FileName);//file name
string path= Server.MapPath(filename);//path

How download manager breaks the file into multiple parts?

I am not clear about the concept of breaking the file into multiple part and then download each part separately. According to me, What we have only the path of that file where it exist on the internet so how to break this file just by knowing the URL or path?
There is a special provision in HTTP 1.1 for this: the Range header, which allows you to fetch a selected portion of the resource. This is exactly what these download managers use.
You can review some code example in Java of partial file download: Resume download in urlconnection. In rfc2616 specified header 'Range' allows to request specified part of file.
So Download Manager simply start partial file downloading in several connection in parallel.

FileUpload control (UploadButton.PostedFile.FileName)

Recently i developed a code on asp that requires to upload a file up to a server.
As i found out from the web, in order to view the local file of a file it can be done by doing UploadButton.PostedFile.Filename.
string fileName = UploadButton.PostedFile.Filename;
This will show the whole local path(eg. C:\Documents and Settings\christopher.lim\Desktop\HelloWorld.txt).
This work fine if it is run the code on my desktop (where my PC is the server itself) but when i shifted the code over to a test server and tried it on my desktop(PC is the client), it only display my file name instead of the whole path.
Example:
string fileName = UploadButton.PostedFile.Filename;
Response.Write("FileName: " + fileName);
1) Local PC -> C:\Documents and Settings\christopher.lim\Desktop\HelloWorld.txt
2) Test Server -> HelloWorld.txt
P/S: Sorry if it's confusing because i am new to server client. Correct me if i'm wrong.
As per MSDN-
The file name that the FileName property returns does not include the
path of the file on the client.
While it is true that on a local system you can get the complete path, but while you run it on a server it will only return the name of the file.
Also FYI the file upload control behaves differently in different browsers. In firefox you ill probably get only the file name and not the full path using fileupload.postedfile.filename and in IE the same thing may show you the full path.
However the path of the file uploaded from client system shouldn't matter as only the file name is more than enough, but if you still have the need try- Path.GetFileName(filename) MSDN link

How to download a file from a UNC mapped share via IIS and ASP

I am writing an ASP application that will serve files to clients through the browser. The files are located on a file server that is available from the machine IIS is running on via a UNC path (\server\some\path).
I want to use something like the code below to serve the file. Serving files that are local to the machine IIS is running on is working well with this method, my trouble is being able to serve files from the UNC mapped share:
//Set the appropriate ContentType.
Response.ContentType = "Application/pdf";
//Get the physical path to the file.
string FilePath = MapPath("acrobat.pdf");
//Write the file directly to the HTTP content output stream.
Response.WriteFile(FilePath);
Response.End();
My question is how I can specify a UNC path for the file name. Also, to access the file share I need to connect with a specific username/password.
I would appreciate some pointers on how I can achieve this (either using the approach above or by other means).
I'm not an ASP guy so I might be completely wrong with these answers.
Regarding the path, I don't think you should be using MapPath, since that's to get a relative path and you already know the physical path so can't you just change that to:
string FilePath = #"\\Server\Directory\FileName.txt";
Regarding the account, I think you need to use impersonation, this link seems to discuss just this:
http://aspalliance.com/336_Upload_Files_Using_ASPNET_Impersonation_and_UNC_Share.all

Getting an incorrect file extension and content type when uploading file

I have an asp.net application where the users can upload files to our database.
Sometimes when they upload the files, the content-type is set as "application/octet-stream" which is a binary file.
When I ask the user, they say that they uploaded they say it was a .tif file. Somehow the upload control sets it as "application/octet-stream".
When I upload the same .tif file from my computer it uploads with the correct content type (application/octet-stream).
I am using the following code to get the file extension
fileExtension = filUpload.PostedFile.FileName.Substring(filUpload.PostedFile.FileName.LastIndexOf(".") + 1)
sometimes it returns the file extension as "c:\documen" or "j:\testing" etc. I know that windows doesn't allow special characters in the filename.
You simply can't rely on the browser to submit a usable MIME media type. The client machine may not have any media type information set up for a particular filetype (which is likely the case for TIFF here), or it may not support sending media types at all, or there may be bugs in it (as there have been in the past with IE).
You also can't rely on the browser to submit a usable filename extension. The client machine may not use file extensions to determine the type of the file. (Indeed, Macs and modern Linux use multiple mechanisms to determine type, so any filename extension may be misleading, if one is present at all.)
For that matter, you can't even rely on the browser to submit a usable filename in the first place! Not every OS uses backslash character and dot for directory and extension separators; the submitted filename is effectively an opaque string which you can use for guessing some of the common cases, but you can't consider to be definitive.
So the only reasonable ways to determine the type of an uploaded file are:
Ask the user explicitly what type they're uploading.
Try to guess what type it might be from media type and trailing filename, falling back to asking the user what type it is.
If the types you want to allow are all ones with sniffable headers (as TIFF and most other image formats are), you can work out the type by looking at the contents of the file.
Use functions in the System.IO namespace to parse it out instead.
To get the extension, you would do this:
fileExtension = System.IO.Path.GetExtension(filUpload.PostedFile.FileName);
I believe that the user's browser sends a declared MIME type along with the file. It's then up to the browser to declare the type of the file. Different browsers may have different ability to infer the best MIME type from a file. When you get the file on your server, you might just check for the .tif[f] extension -- that's probably all the checking that the uploading browser will do anyways.

Resources