Reputation: 41
i have a file upload in my code. I want to save that file in different name (i.e patient id + timestamp) but i could not save the uploaded file in actual physical location. its shows format is not correct.
here location and deptcode taken from session.and mainpath is taken from variable defined in web.config. thats base path. from that base path i am creating sub folders is its not there and then have to save uploaded file in different name. values are all correct.even i could create subfolders if its not there ,but only problem is saveas line. i cannot save uploaded file. here if i use SaveAs(Server.MapPath(..)) it shows u should use virtual path. if i use only saveas(filelocation) then it shows format is wrong..please help me..
Upvotes: 0
Views: 2324
Reputation: 470
try
{
FbDataAdapter daa = new FbDataAdapter();///////////////////time :1.00 to 9.00
string da = DateTime.Now.ToString();
string date = da.Substring(0, 2);
string date1 = da.Substring(3, 4 - 2);
string date2 = da.Substring(6, 9 - 5);
string daietime = "D" + date + "M" + date1 + "Y" + date2;
int f = da.IndexOf(":");
int l = da.Length;
string main = da.Substring(11, f - 11);
string refer = da.Substring(13, f - 10);
string refer1 = da.Substring(16, f - 10);
string refer2 = da.Substring(19, f - 10);
string time = "H" + main + "M" + refer + "S" + refer1 + "S" + refer2;
// uploadedfilename means FileUpload Control
uploadedfilename.SaveAs(@Request.PhysicalApplicationPath + "images\\"+ daietime + time + Path.GetExtension(uploadedfilename.FileName));
}
catch
{
string da = DateTime.Now.ToString();//////////////////////// Time :10.00 to 12.00
string date = da.Substring(0, 2);
string date1 = da.Substring(3, 4 - 2);
string date2 = da.Substring(6, 9 - 5);
string daietime = "D" + date + "M" + date1 + "Y" + date2;
int f = da.IndexOf(":");
int l = da.Length;
string main = da.Substring(11, f - 11);
string refer = da.Substring(14, f - 11);
string refer1 = da.Substring(17, f - 11);
string refer2="";
string time="";
try
{
refer2 = da.Substring(20, f - 11);
time = "H" + main + "M" + refer + "S" + refer1 + "S" + refer2;
}
catch
{
refer2 = "H" + main + "M" + refer + "S" + refer1;
time = "H" + main + "M" + refer + "S" + refer1 + "S" + refer2;
}
uploadedfilename.SaveAs(@Request.PhysicalApplicationPath + "images\\"+ daietime + time + Path.GetExtension(uploadedfilename.FileName));
}
Upvotes: 0
Reputation: 8109
string timestamp = DateTime.Now.ToString();
this return some thing like this 5/15/2013 10:14:18 AM
. A window folder And File Can not Contain following characters:
\ / : * ? " < > |
and your DateTime.Now
returing one of this this character that is : and /
that why its giving you this error.Remove :
by DateTime.Now.ToString().Replace(':','-').Replace('/','-');
i think it will work.
Upvotes: 2
Reputation: 2527
The problem is because of the ":" from your DateTime.Now.ToString(). You can use the ToString() method with string format to change it. Example:
DateTime.Now.ToString("dd-MM-yy-hh-mm-ss")
This should solve the problem
Upvotes: 1
Reputation: 12705
you should change
string timestamp = DateTime.Now.ToString();
to
string timestamp = DateTime.Now.Ticks.ToString();
or to
string timestamp = DateTime.Now.ToString("yyyyMMdd_hhmmss");
this will append a long number converted to string and therefore it will generate a valid name and also you will have a valid timestamp
Upvotes: 0