I am not able to save image into Folder - asp.net

I am trying to save a image in specified folder but it doesn't saving image but code is working for excel file storing...did i missed something here....please suggestions needed.
string filename = Path.GetFileName(imgUploadControl.PostedFile.FileName);
string folder = Server.MapPath("~/Files/");
imgUploadControl.PostedFile.SaveAs(Path.Combine(folder, filename));
string filePath = Path.Combine(folder, filename);
imglogo.ImageUrl = Server.MapPath("~/Files/" + imgUploadControl.FileName);

Related

Why i can not find file on the server for download in asp.net?

I'm beginner in asp.net and want to write simple web application to end user can download any file,write this code for that purpose:
string filePath = "~/beh/" + query[0].OstFileName;
FileInfo file = new FileInfo(filePath);
if (file.Exists) {
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + query[0].OstFileName.Trim());
Response.TransmitFile(Server.MapPath("~/beh/" + query[0].OstFileName.Trim()));
Response.End();
} else {
Response.Write("<script>alert('File Not Found 1')</script>");
}
but when run that code i get else block alert,means get File Not Found 1 message,Where is my fault?thanks all.
string filePath = "~/beh/" + query[0].OstFileName;
In this line you specify the path to search for, including the filename.
The next line you check if this file exists and if so save the file from the query result.
However if you check if the file exists before you actually save the file it will never exist.
Change the path to read: string filePath = Server.MapPath("~\beh").
Then add the filename to that once you save the file.
Hope this makes sense.
Your issue is in the following lines.
string filePath = "~/beh/" + query[0].OstFileName;
FileInfo file = new FileInfo(filePath);
You're giving the FileInfo class a virtual path which the FileInfo cannot map to a local physical path on its own. You are however correctly mapping the virtual path to the local file path when you call Response.TransmitFile.
Change your code to the following:
string filePath = "~/beh/" + query[0].OstFileName;
FileInfo file - new FileInfo(Server.MapPath(filePath));
See: https://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx

DirectotyNotFoundexception when i upload image through Fileupload tool in Asp.net

i want to store the images to the ms SQL server,when i am submit the button after inserting the image i getting Directory Not Found exception the error.
Thanks in advance.
if(filuploadimages.PostedFile!=null)
{
string filename = filuploadimages.PostedFile.FileName.ToString();
string fileExt = System.IO.Path.GetExtension(filuploadimages.FileName);
filuploadimages.SaveAs(Server.MapPath("~/Productimages/" + filename));//error
}
Which path are you getting as result from this line?
Server.MapPath("~/Productimages/" + filename)
I suggest you to try to get the upload path in a different way:
if (filuploadimages.PostedFile != null)
{
string filename = System.IO.Path.GetFileName(filuploadimages.PostedFile.FileName);
string uploadPath = System.IO.Path.Combine(Server.MapPath("~/Productimages"), filename);
filuploadimages.SaveAs(uploadPath);
}

Download upload file from specific folder

I want to download upload file which is inside a folder into my project..
My Database Table
My Upload Code is
string filename = Path.GetFileName(FileUploadPatDoc.FileName);
string pathName = Path.GetFullPath(FileUploadPatDoc.PostedFile.FileName);
FileUploadPatDoc.SaveAs(Server.MapPath("~/PatientDocuments/") +filename);
fsDocuments.FileName = filename; // fsDocument is Table Object name
fsDocuments.FilePath = pathName;
It works nicely and Store documents in "PatientDocuments" folder which is inside my project.
Now when I want to download it from Gridview using Image-button that times it couldn't find it's destination Path.
Here is the Upload code
ImageButton Btn = (ImageButton)sender;
GridViewRow row = (GridViewRow)Btn.NamingContainer;
string fileName, filePath;
Label file = (Label)grdPatientDocument.Rows[row.RowIndex].FindControl("lblFileName");
Label path = (Label)grdPatientDocument.Rows[row.RowIndex].FindControl("lblFilePath");
if (file != null)
{
fileName = file.Text;
filePath = path.Text;
Response.ContentType = filePath;
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
Response.TransmitFile(Server.MapPath(fileName));
Response.End();
}
It generate following error
"Could not find file E:\CoreZ
IT\June\Hospital\22-06\Final\CZ_BHC\CZ_BHC\BHC\CV_MD.Golam_Shahriar.pdf'"
But I need to find this file from E:....\PatientDocuments\CV_MD.Golam_Shahriar.pdf
Please help me and I am using VS 2012... Thanks
Try this one
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + "");
Response.TransmitFile(filePath);
Response.End();

File already exist using system.io.file.copy

I'm using System.IO.File.Copy to copy files from serverA to serverB. This works fine accept when the file exsist I get an error "File already exist". I tried trapping it using if file.exsist and nothing.
here is my code.
'Save files to disk
FileUpload1.SaveAs(Server.MapPath("../pdf/audits/" & FileName))
'Local Server
Dim localPath As String = "\\server01\folder1$\pdf\audits\"
'Remote Server
Dim remotePath As String = "\\server02\folder2$\pdf\audits\"
System.IO.File.Copy(localPath + FileName, remotePath + FileName)
What am I missing?
If you just modify your copy operation like this, it should work. The last parameter will overwrite the file.
System.IO.File.Copy(localPath + FileName, remotePath + FileName, True);
If you have large files, you are not going to want to overwrite them everytime. Try fixing your check to see if the file exist. Something like this (C#):
var localPath = #"C:\";
var remotePath = #"\\server\folder\";
var fileName = "test.txt";
if (!new System.IO.FileInfo(remotePath + fileName).Exists)
{
System.IO.File.Copy(localPath + fileName, remotePath + fileName);
}
There's a third parameter to overwrite if it already exists
System.IO.File.Copy(fileName, destName, overwrite);
I got it working with help from RLG.
'Save files to disk
FileUpload1.SaveAs(Server.MapPath("../pdf/audits" & FileName))
'SIGAR Public CMS
Dim localPath As String = "\\hqdadev01\sigar_cms$\pdf\audits\"
'SIGAR Dev
Dim remotePath As String = "\\hqdadev02\sigar_public$\pdf\audits\"
Added this to check.
If Not New System.IO.FileInfo(remotePath + FileName).Exists Then
File.Copy(localPath + FileName, remotePath + FileName, overwrite)
End If

how to get the full path of file upload control in asp.net?

i am using asp.net 2.0 in my project using file upload control , so browse the drive and select the file like path(D:\user doc new 2011\Montana\MT_SUTA_2010-2011.html)
but in my code seeing error is could not find the file path is
(D:\Paymycheck\OnlineTaxUserDocumentation-1\TaxesDocument\MT_SUTA_2010-2011.html) actually it is application path and take the filename only my code is
if (FileUpload.HasFile)
{
string filepath = Server.MapPath(FileUpload.FileName);
string strHTML = File.ReadAllText(filepath);
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] file1 = encoding.GetBytes(strHTML);
int len = file1.Length;
byte[] file = new byte[len];
docs.TaxAuthorityName = ddlTaxAuthority.SelectedItem.Text;
docs.TaxTypeID = ddlTaxType.SelectedValue;
docs.TaxTypeDesc = ddlTaxType.SelectedItem.Text;
docs.EffectiveDate = Convert.ToDateTime(txtEffectiveDate.Text);
docs.FileName = f1;
if (ddlTaxAuthority.SelectedValue == "FD")
{
docs.Add(strHTML, file1);
}
}
error occoured in this line
string strHTML = File.ReadAllText(filepath);
i can try like this also
string FolderToSearch = System.IO.Directory.GetParent(FileUpload.PostedFile.FileName).ToString();
string f = Path.GetDirectoryName(FileUpload.PostedFile.FileName);
string f1 = FileUpload.FileName;
string filepath = System.IO.Path.GetFullPath(FileUpload.PostedFile.FileName);
string strFilePath = FileUpload.PostedFile.FileName;
string file1234 = System.IO.Path.GetFullPath(FileUpload.PostedFile.FileName);
string filepath = FileUpload.PostedFile.FileName;
so how to get the total path drive to file
pls help me any one
thank u
hemanth
Because your are using Server.MapPath, which according to MSDN "maps the specified relative or virtual path to the corresponding physical directory on the server." You have to first call FileUpload.SaveAs method to save file on server then try to read it's content.
If you want the total client side drive path like D:\user doc new 2011\Montana\MT_SUTA_2010-2011.html for the file MT_SUTA_2010-2011.html to be uploaded via fileupload control, then try using System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName).
This will surely return the client side path of the file.
Try this it will work
string filepath = System.IO.Path.GetFullPath(fuldExcel.PostedFile.FileName);
fuldExcel.SaveAs(filepath); //fuldExcel---is my fileupload control ID

Resources