DirectotyNotFoundexception when i upload image through Fileupload tool in Asp.net - 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);
}

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

how to send an image via email without saving it to a folder before

I am uploading images to an asp.net website using fileupload and i need to send the uploaded images via email to do this i am saving the image to a folder on the erver and then i am sending the image but can i send it without saving it to a folder before or if i need to save it to a folder can i delete it as soon as it's sent?
thanks.
my code:
string filename = Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath("UploadImages/" + filename));
string attachmentPath = Server.MapPath("UploadImages/" + filename);
Attachment inline = new Attachment(attachmentPath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentType.Name = Path.GetFileName(attachmentPath);
mail.Attachments.Add(inline);
using System.Net.Mail;
[WebMethod]
public void SendFile(HttpPostedFileBase file)
{
//...setup mail message etc
Attachment attachment = new Attachment(file.InputStream, file.FileName);
message.Attachments.Add(attachment);
}

Image path is not store in table field as a string from fileupload

i have use ascx it contain fileupload control to get the selected image path as a string in data base table filed using linq, and ascx is used in dotnetnuke module page.
there is code which i have use in my ascx.cs but it not get the image path from fileupload control
FormViewRow row = FormView1.Row;
FileUpload FileUpload1 = (FileUpload)row.FindControl("FileUpload1");
string filename = "";
string path;
if (FileUpload1.HasFile)
{
filename = FileUpload1.PostedFile.FileName;
path = Server.MapPath("DesktopModules/IndiesStore/Images/")+
FileUpload1.FileName;
string onlyname = path.Substring(path.LastIndexOf("\\") + 1);
IC.Img = "DesktopModules/IndiesStore/Images/" + onlyname;
}
there is an any solution of this....?
ya but it worked directly like that code
FormViewRow row = FormView1.Row;
FileUpload FileUpload1 = (FileUpload)row.FindControl("FileUpload1");
string filename = "";
if (FileUpload1.HasFile)
{
filename = FileUpload1.PostedFile.FileName;
IC.Img = "DesktopModules/IndiesStore/Images/" + filename;
}
so it is solved,
user have to just place the all images on same folder on server and select the images in fileupload from there so it work properly.

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