Asp.net finding and downloading file - c#

I have File Upload form which users use to upload certain files on the server. The files are uploaded in path that look like this.
"G:\\VS\\Ticketing System2\\UploadedFiles\\" + ProjectId + "\\" + ticketId + "\\TicketFiles\\";
After that I have a repeater which displays some data and have a hyperlink. I want to name the hyperlink"Download files(" + fileCount + ")" and onclicked it should display a regular Save As window. Can you give me some code to do that. I've never done something like this.

To get the number of files in the directory, you need to use IO. Then
string path = #"G:\\VS\\Ticketing System2\\UploadedFiles\\" + ProjectId + "\\" + ticketId + "\\TicketFiles\\";
int numFiles = Directory.GetFiles(path).Length;
Then in your page_load, just change to .Text of the link to include the numFiles variable

Related

Set ImageURL without knowing the file extension c#

I have a folder where images are uploaded and named 'Photo1', 'Photo2', etc depending on how many photos are uploaded. In another panel on the same aspx page I want to display the uploaded photos. I will know the filenames but the extension could be .png, .jpg, or .jpeg.
How can I set the ImageUrl path when I know only the filename and not the extension?
Here is an example of how I've attempted it so far--
ASPX Page Code:
<asp:Image ID="Image1" runat="server" />
Code Behind:
Image1.ImageUrl = Server.MapPath("~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.*");
This method says I've used an invalid character. Although, I've tested it by going into the folder and finding the extension then entering the line as:
Image1.ImageUrl = Server.MapPath("~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.jpg");
and that didn't work either, so there may be more wrong here than just the unknown character in the extensions place.
string jpg = "~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.jpg"
string png = "~/RepairPhotos/" + order_id + "." + unit_id + ".RepairPhoto1.png"
if (File.Exists(Server.MapPath(jpg)))
{
}
else if(File.Exists(Server.MapPath(png)))
{
}
If you don't have the file's extension, you may need to read the first few bytes of that file to determine if an image is in what format.
This snippet may help: https://gist.github.com/ChuckSavage/dc079e21563ba1402cf6c907d81ac1ca

Spreadsheetlight save as working too slow

I am working on a winform with spreadsheetlight library.I want to create a excel file with specific name under a specific folder when user clicked on "Save to Excel" button. Ihave tried the below code so far.
string path = System.IO.Path.GetTempPath();
sl.SaveAs(path + "\\" + dosyaismi + ".xlsx");
FileInfo fi = new FileInfo(path + "\\" + dosyaismi + ".xlsx");
if (fi.Exists)
{
System.Diagnostics.Process.Start(#path + "\\" + dosyaismi + ".xlsx");
}
else
{
MessageBox.Show("Dosya bulunamadı!");
}
When program run and clicked that button first time it takes 2 minutes to create file.But without closing program I clicked that button again with different filename it creates file immediately.
I searched for 2 hours but ı didn't find any solution.Can anyone help what is the problem?

How to rename a file while uploading

I am uploading file.so I want to change my file name.
string createfolder = "E:/tmp/jobres/" + uId;
System.IO.Directory.CreateDirectory(createfolder);
string newfilename = txtname.Text + "Resume" + fileExtension;
AsyncFileUpload1.SaveAs(Path.Combine(createfolder,newfilename ));
This time file is in different format then its not replacing.
string createfolder = "E:/tmp/jobres/" + uId;
System.IO.Directory.CreateDirectory(createfolder);
//AsyncFileUpload1.SaveAs(Path.Combine(createfolder,"Give Something Here whatever is changing at all time like time value"+AsyncFileUpload1.PostedFile.FileName));
AsyncFileUpload1.SaveAs(Path.Combine(createfolder,"DateTime.Now"+AsyncFileUpload1.PostedFile.FileName));

performance degradation after 30-40 mins c#

I'm trying to download files from a remote location. But right before the download, I get my file locations from a web service, also on a remote location.
The thing is, I get a degrading performance over time. The downloaded file numbers decrease from around 2k in 3 minutes to 300-400 in the same time after an hour or two and I have 250k files.
Is the service or the download a problem? Or both?
I download files as below after I get the names from the service,
try
{
using (WebClient client = new WebClient())
{
if (File.Exists(filePath + "/" + fileName + "." + ext))
{
return "File Exists: " + filePath + "/" + fileName + "." + ext;
}
client.DownloadFile(virtualPath, filePath + "/" + fileName + "." + ext);
return "Downloaded: " + filePath + "/" + fileName + "." + ext;
}
}
catch (Exception e) {
return"Problem Downloading " + fileName + ": " + e.Message;
}
if (File.Exists(filePath + "/" + fileName + "." + ext))
Bottleneck is probably here.
When you get hella tons of files in a single folder checking if file with this name already exists might need some time to complete.
So you might want to store files in different folders
The problem was the information put on the richTextBox and label.
The rtb was appended with info regarding whatever happened to each individual element. The Label showed at which element we were. Apparently the cpu can't handle it and this becomes a major problem when running for prolonged time. It ate so much cpu that it eventually killed the application. Removing or limiting their output solved almost all problems.
On the other hand, the slight degradation (1.5k to 1.2k per minute after 2.5 hours) of the download that still exists is still a mystery.

Get Path in asp.net for c#

I am trying to get a path for a file which a users uploads but the path that I am getting is wrong because the path should be for an example
"C:\\Users\\Tranga.Patel\\Downloads\template.xlsx"
but on my program I get
"c:\\windows\\system32\\inetsrv\\Template Final.xlsx"
the code that I am using is
fileName = Path.GetFullPath(fileUpload.PostedFile.FileName);
I also tried using
fileName = Server.MapPath(Path.GetFullPath(fileUpload.PostedFile.FileName));
this give the directory of the project
try using following:
var path=Server.MapPath('Uploads folder path from root directory');
This gives you the folder path from the root directory of your website.
EDIT:- You should be knowing to which path users are saving the file if it is not in your site directory tree.
Are you using a File Upload control? If you are, you just need them to select the document they want to upload and then specify the path you want to save it at. For example
// Get File Name
documentName = Path.GetFileName(fuContentDocuments.FileName);
// Specify your server path
string serverPath = Server.MapPath("../../" + WebConfigurationManager.AppSettings["FilePath"].ToString());
// The final path
string fileLocation = (serverPath + "\\" + userId + "\\" + documentName);
// if folder doesn't exist then create it
if (!Directory.Exists(serverPath + "\\" + userId + "\\"))
{
// create the folder for the file
Directory.CreateDirectory(serverPath + "\\" + userId + "\\");
}
// Upload the file
fuContentDocuments.SaveAs(fileLocation);
Note: UserId is just the users login userId. This way other users wont override it.

Categories

Resources