How to replace the file name(particular specific folder) using Asp.net? - asp.net

i can replace the file name in particular folder , i wrote like this
FileInfo fsource = new FileInfo(Server.MapPath("~/PurchaseOrder/" + lblhideid.Text));
if (fsource.Exists)
{
string[] file = lblhideid.Text.Split('.');
string fName="Z-"+System.DateTime.Now.ToString("MM-dd-yyyy")+"-"+saveConsultantID+"."+file[1];
fsource.Name.Replace(lblhideid.Text, fName);
}
lblhideid.Text=image.jpeg , so i can replace the my own name like fName , how to replace the name pls give me any suggestion.
Thank u
Hemanth

I suspect you want that last line to be:
fsource.MoveTo(Server.MapPath("~/PuchaseOrder/" + fName));
You current code is only getting the filename as a string and manipulate that string. You want to manipulate the file itself.
EDIT:
Are you sure that ~/PurchaseOrder/ exists?
Try:
string originalPath = Server.MapPath("~/PurchaseOrder/" + lblhideid.Text);
FileInfo fsource = new FileInfo(originalPath);
if (fsource.Exists)
{
string newName = string.Format("Z-{0:MM-dd-yyyy}-{1}.{2}",
System.DateTime.Now,
saveConsultantID,
fsource.Extension);
string newPath = Path.Combine(fsource.DirectoryName, newName);
fsource.MoveTo(newPath);
}

Try this, what if they put a filename like file.tar.gz ?
string extension = Path.GetExtension("~/PurchaseOrder/" + lblhideid.Text);
string newName = "MYFILE." + extension
File.Move(
"~/PurchaseOrder/" + lblhideid.Text,
"~/PurchaseOrder/" + newName );

Related

how can i recover the error(Could not find a part of the path 'E:\Work Station\Works\NewThrissurDiary\images\'.) that i got in my update query?

I want to update the files which I uploaded to database
This is my query.. please help...
if (FileUpload1.HasFile || FileUpload2.HasFile || FileUpload3.HasFile || FileUpload4.HasFile || FileUpload5.HasFile)
{
string filename1 = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("~/images/" + filename1));
string filename2 = Path.GetFileName(FileUpload2.PostedFile.FileName);
FileUpload2.SaveAs(Server.MapPath("~/images/" + filename2));
string filename3 = Path.GetFileName(FileUpload3.PostedFile.FileName);
FileUpload3.SaveAs(Server.MapPath("~/images/" + filename3));
//string filename4 = Path.GetFileName(FileUpload4.PostedFile.FileName);
//FileUpload4.SaveAs(Server.MapPath("~/images/" + filename4));
string filename5 = Path.GetFileName(FileUpload5.PostedFile.FileName);
FileUpload5.SaveAs(Server.MapPath("~/images/" + filename5));
}
even It is an old Topic....
But I have recently gotten same Error... The Error Try to access the pathe which is not available. Let me say as my example :
I Have a folder (Fa) which belongs to My Experts and The main Path where records upload files are In another folder (Fb)...
At First I wrote this Code :
string strname2;
strname2 = ("Fa/" + FileUpload2.PostedFile.FileName.Substring(FileUpload2.PostedFile.FileName.LastIndexOf("//") + 1));
LabelFile.Text = strname2;
and when I use Following Error I got that deadly Error :
if (strname2 != "Fa/")
{
FileUpload2.PostedFile.SaveAs(Server.MapPath(strname2));
}
else
{
LabelFile.Text = "";
}
After some test and try I change both above code to following Codes and Guess what? Everything works like a charm :
1:
string strname2;
strname2 = ("../Fb/Fa/" + FileUpload2.PostedFile.FileName.Substring(FileUpload2.PostedFile.FileName.LastIndexOf("//") + 1));
LabelFile.Text = strname2;
And then:
2:
if (strname2 != "../Fb/Fa/")
{
FileUpload2.PostedFile.SaveAs(Server.MapPath(strname2));
}
else
{
LabelFile.Text = "";
}
Now the solution is check your folders and see that folders are available or not.
PS: Search specially for this folder : NewThrissurDiary
Hope this Help you

How do I pass in feed credentials to XDocument that allows Linq to XML to work

I can take the results of my variable xmlDoc and save it to an xml document (on disk) and the query returns results; however, when I run it live (with the code below) the query results are null.
Why is my query not working against the xmlDoc?? See screen shot below for the debug output 'xmlDoc.Root'.
There must be a cleaner way to create the XDocument??
System.Net.WebClient wc = new System.Net.WebClient();
wc.Credentials = new System.Net.NetworkCredential("bagee18#gmail.com", "my_password");
XDocument xmlDoc = XDocument.Parse(wc.DownloadString(new Uri("https://mail.google.com/mail/feed/atom")), LoadOptions.None);
var q = (from c in xmlDoc.Descendants("entry")
select new
{
name = c.Element("title").Value,
url = c.Element("link").Attribute("href").Value,
email = c.Element("author").Element("email").Value
}).ToList();
q.Dump();
xmlDoc.Root:
.
Take the namespace into account:
XNamespace df = xmlDoc.Root.Name.Namespace;
var q = (from c in xmlDoc.Descendants(df + "entry")
select new
{
name = c.Element(df + "title").Value,
url = c.Element(df + "link").Attribute("href").Value,
email = c.Element(df + "author").Element(df + "email").Value
}).ToList();

ado.net query removes file exention when adding string filename to the database

I am using the code below for saving uploaded picture and makigna thumbnail but it saves a filename without the extension to the database, therefore, I get broken links. How can I stop a strongly typed dataset and dataadapter to stop removing the file extension? my nvarchar field has nvarchar(max) so problem is not string length.
I realized my problem was the maxsize in the dataset column, not sql statement parameter, so I fixed it. You may vote to close on this question.
hasTableAdapters.has_actorTableAdapter adp1 = new hasTableAdapters.has_actorTableAdapter();
if (Convert.ToInt16(adp1.UsernameExists(username.Text)) == 0)
{
adp1.Register(username.Text, password.Text,
ishairdresser.Checked, city.Text, address.Text);
string originalfilename = Server.MapPath(" ") + "\\pictures\\" + actorimage.PostedFile.FileName;
string originalrelative = "\\pictures\\" + actorimage.FileName;
actorimage.SaveAs(originalfilename);
string thumbfilename = Server.MapPath(" ") + "\\pictures\\t_" + actorimage.PostedFile.FileName;
string thumbrelative = "\\pictures\\t_" + actorimage.FileName;
Bitmap original = new Bitmap(originalfilename);
Bitmap thumb=(Bitmap)original.GetThumbnailImage(100, 100,
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback),
IntPtr.Zero);
thumb=(Bitmap)original.Clone(
new Rectangle(new Point(original.Width/2,original.Height/2), new Size(100,100)),
System.Drawing.Imaging.PixelFormat.DontCare);
/*
bmpImage.Clone(cropArea,bmpImage.PixelFormat);
*/
thumb.Save(thumbfilename);
adp1.UpdatePicture(originalrelative, thumbrelative, username.Text);
LoginActor();
Response.Redirect("Default.aspx");
}
}
Looks like the problem is you are using HttpPostedFile.FileName property, which returns fully-qualified file name on the client. So, this code string originalfilename = Server.MapPath(" ") + "\\pictures\\" + actorimage.PostedFile.FileName; generates something like this:
c:\inetpub\pictures\c:\Users\Username\Pictures\image1.jpg
Use FileUpload.FileName property everywhere and you will probably get what you want.
Use this to get Image or file extension :
string Extension = System.IO.Path.GetExtension(FileUpload.FileName);

java.io.FileNotFoundException?

i have written the code of jsp,servlet for uploading the Doc file in database.
here is my code,but i am getting the error like this==java.io.FileNotFoundException: insert into resume(resume) values(?) (The filename, directory name, or volume label syntax is incorrect) .please help me how to remove this error???
try
{
Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:jtds:sqlserver://W2K8SERVER:1433/career","sa","Alpha#123" );
pst = con.prepareStatement("select * from data1 where email=? and password=?");
pst = con.prepareStatement
("insert into resume(resume) "+ "values(?)");
File re = new File("" + pst);
fis = new FileInputStream(re);
pst.setBinaryStream(3, (InputStream)fis, (int)(re.length()));
pst.setString (1,upload);
//rs = pst.executeQuery();
while (rs.next())
cnt ++;
int s = pst.executeUpdate();
if(s>0) {
System.out.println("Uploaded successfully !");
}
else {
System.out.println("unsucessfull to upload image.");
}
rs.close();
pst.close();
con.close();
}
It is because insert into resume(resume) values(?) is probably not the name of a file on your disk.
pst = con.prepareStatement ("insert into resume(resume) "+ "values(?)");
File re = new File("" + pst);
You are creating a File from ""+ pst, which is the result of toString() being called on a PreparedStatement. Did you put in the "" to get rid of the informative compilation error?
You probably have the real filename in some other variable.
File re = new File(theRealFileNameVariable);
File re = new File("" + pst);
please make sure the "pst" value is a valid file path, apparently the value "insert into resume(resume) values(?)" is not a valid file path
The java.io.File class has four (4) constructors
File(File parent, String child)
File(String pathname)
File(String parent, String child)
File(URI uri)
In your case you are trying to pass to the constructor an object or type java.sql.PreparedStatement that you trying to cast into a String. I don't see how (even if you arrived to convert pst into a string) pst will reference a path to a file. Please go back and read a little bit about java.io.
.

Virtual path exception in asp.Net

I have to save files to a physical folder. But i got the following exception. "The path is not a virtual path" How can i change a physical path to a virtual path. How can i solve this problem
Grades grade = new Grades();
grade.Exam = Exams.Search(ddlExam.SelectedValue);
grade.Person = Person.GetStudent(ddlStudent.SelectedValue);
try{
if (afuPaper.HasFile)
{
string strPath = Server.MapPath(grade.Exam.FileUrl) + ddlExam.SelectedValue +
grade.Person.TcNo + Path.GetFileName(afuPaper.FileName);
afuPaper.SaveAs(strPath); grade.GradeJpeg = strPath;
}
}
Grades grade = new Grades();
grade.Exam = Exams.Search(ddlExam.SelectedValue);
grade.Person = Person.GetStudent(ddlStudent.SelectedValue);
try
{
if (afuPaper.HasFile)
{
string strPath = Server.MapPath(grade.Exam.FileUrl) + ddlExam.SelectedValue + grade.Person.TcNo + Path.GetFileName(afuPaper.FileName);
afuPaper.SaveAs(strPath);
grade.GradeJpeg = strPath;
}
grade.Exam.FileUrl is a pyhsical path like C:\File\Paper1
Server.MapPath expects a virtual path and converts it to a physical path.
Assuming you require a physical path for SaveAs, then you can simply remove that call.
The MapPath method is used to convert a virtual path, such as "~/Location/File.aspx" to a physical path such as "C:\inetput\wwwroot\MyApplication1\Location\File.aspx"
EDIT: Modified code
try
{
if (afuPaper.HasFile)
{
string strPath = Path.Combine(grade.Exam.FileUrl, ddlExam.SelectedValue + grade.Person.TcNo + Path.GetFileName(afuPaper.FileName));
afuPaper.SaveAs(strPath);
grade.GradeJpeg = strPath;
}
Note that I also included a call to Path.Combine, which is the method of choice for combining a folder and file name (It will automatically add a \ if needed between FileUrl and whatever)

Resources