Selecting Folder name for saving files

Posted by Shoyebaziz123 under ASP.NET on 9/4/2013 | Points: 10 | Views : 2081 | Status : [Member] | Replies : 3
Hi All,

I have a problem that how i select a folder in my computer to save my file ?

means

there is selection for folder ( user can select any folder of any directory C/D/E and inside that he/ she select a folder) then how can i achive this folder path ...


Thanks




Responses

Posted by: Bandi on: 9/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");

response.TransmitFile(FilePath);


TransmitFile will causes to prompt user for Open/Save file options

Refer these links:
http://stackoverflow.com/questions/10818031/prompt-user-to-save-open-file-in-asp-net-c-sharp
http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/4/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
//Sample code

// add below namespaces
using System.IO; 

using Microsoft.Win32;

protected void Page_Load(object sender, EventArgs e)
{
var file = Request.QueryString["file"];
var hasFile = !String.IsNullOrWhiteSpace(file);

if(!hasFile)
return;

var hasFileExists = File.Exists(Server.MapPath(file));
if (!hasFileExists)
return;

var filepath = Server.MapPath(file);
var filename = Path.GetFileName(filepath);
var contentType = GetContentType(filepath);


var hasValidFileExtension = IsValidFileExtension(filepath);
var hasReadyForDownLoad = hasValidFileExtension && !String.IsNullOrWhiteSpace(filename) && !String.IsNullOrWhiteSpace(contentType);

if (!hasReadyForDownLoad)
return;

HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.ContentType = contentType;
response.AddHeader("Content-Disposition", "Attachment; filename=" + filename + ";");
response.TransmitFile(filepath);
response.Flush();
response.End();

}

// get content type
private string GetContentType(string filepath)
{
var contentType = "application/octetstream";
var extension = Path.GetExtension(filepath);

RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(extension);

if (registryKey != null && registryKey.GetValue("Content Type") != null)
contentType = registryKey.GetValue("Content Type").ToString();

return contentType;
}

// check is file have valid extension
private bool IsValidFileExtension(string file)
{
bool isValidFileExtension = false;

string[] VideoFileExtensions = new string[] {"avi", "mpeg","wmv", "mp4" };
string[] ImageFileExtensions = new string[] { "jpeg", "jpg", "gif", "png" };
string[] AudioFileExtensions = new string[] { "mp3", "aac", "wma", "wav" };

string extension = Path.GetExtension(file).TrimStart('.');

if (VideoFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
{
isValidFileExtension = true;
}
else if (ImageFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
{
isValidFileExtension = true;
}
else if (AudioFileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
{
isValidFileExtension = true;
}


return isValidFileExtension;
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Have you got solution for the above thread... if yes mark it as answered; otherwise post us back

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shoyebaziz123, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response