After deployment first time image display and same page next time change the imageurl old image only is there new image not display. this is code i want change anything in code....
protected void btn_Upload_Click(object sender, EventArgs e)
{
Image1.Dispose();
if (FileUpload1.HasFile)
{
if (!string.IsNullOrEmpty(txtDesignNo.Text))
{
Image1.Visible = true;
Image1.Dispose();
Image1.ImageUrl = string.Empty;
filename = System.Web.HttpContext.Current.Server.MapPath("") + "\\Images" + "\\" + txtDesignNo.Text + ".jpg";
// filename =CGlobals.imagePath + txtDesignNo.Text + ".jpg";
// string filename = System.Web.HttpContext.Current.Server.MapPath("") + "\\ImageStorage" + "\\" + 1 + ".jpg";
FileUpload1.SaveAs(filename);
Image1.ImageUrl = "~/Masters/Images/" + txtDesignNo.Text + ".jpg";
// Image1.ImageUrl =CGlobals.imagePath + txtDesignNo.Text + ".jpg";
//Image1.ImageUrl = filename;
}
}
}
Try shift+f5 to force a full refresh. Depending on your browser, this might force the fetching of the new image instead of using the cached version.
Looks like you save the image under \Images\ but then you try to show it from \Masters\Images.
Try changing the upload path to match the imageurl or vice versa.
try to check the image path using firebug. You saved image to image folder, but you are accessing from master\images.
Related
I thought that should be simple, yet I can't figure it out.
I keep getting the error: System.IO.DirectoryNotFoundException: Could not find a part of the path "/storage/emulated/0/Pictures/Screenshots/name.jpg".
The code:
string root = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).Path;
File myDir = new File(root + "/Screenshots");
myDir.Mkdirs();
string timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").Format(new Date());
string fname = CommunicationHandler.GetNickname() + "|" + timeStamp + ".jpg";
File file = new File(myDir, fname);
if (file.Exists())
file.Delete();
try
{
using (System.IO.Stream outStream = System.IO.File.Create(file.Path))
{
finalBitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, outStream);
outStream.Flush();
outStream.Close();
}
}
catch (Exception e)
{
Toast.MakeText(Activity, e.ToString(), ToastLength.Long).Show();
}
Also, I can't access manually to /storage/emulated/0..
Why can't I manage to save the bitmap to my phone gallery? What's the problem in the code above?
If you want to create a new directory, you can use System.IO.Directory.CreateDirectory(root); to create it.
//create a directory called MyCamera
string root = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDcim).ToString() + "/MyCamera/";
//create the Directory
System.IO.Directory.CreateDirectory(root);
As taken from here:
MediaScannerConnection.scanFile(this, new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
}
Also, note that you could just add an image to the gallery via a simple line:
MediaStore.Images.Media.insertImage(applicationContext.getContentResolver(), IMAGE ,"nameofimage" , "description");
please help me. I would like to create new folder and save a screenshots from selenium therein.
I want, when I click the button xxx_1, folder will automatically be created with text which I enter in txt_Box1 and currently date.
Folder should be looks like that:
Test_test2_18_test3-test4_test5_test_11-Jul-2017
Here's my code
private void xxx_1(object sender, EventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
+ "C:/xxx/xxx" + "_" + textBox1 + "_" + "xxx_xxx_xx_" + DateTime.Now;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//string path = #"C:\\xxx\xxx" + "_" + textBox1 + "_" + "xxx_xxx_xxx_" + DateTime.Now;
String xxx= "https://xxx.xxx.xxx";
IWebDriver driver_xx = new ChromeDriver();
driver_xx.Navigate().GoToUrl(xxx);
driver_xx.FindElement(By.Id("xxx")).SendKeys("xxx");
driver_xx.FindElement(By.Id("xx")).SendKeys("xxx");
driver_xx.FindElement(By.Id("xx")).Click();
Thread.Sleep(3000);
Screenshot ss_xx = ((ITakesScreenshot)driver_xx).GetScreenshot();
ss_xx.SaveAsFile("How to save the screenshots in new created folder??", OpenQA.Selenium.ScreenshotImageFormat.Jpeg);
}
You can't use a DateTime in your path like that as the default implementation of .ToString() on a DateTime will contain invalid characters. Use a format specifier:
string path = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"xx\\xx",
textBox1.Text,
"xx_xxx_xxx_",
DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss") // This will show '21-09-2017 16-11-15'
);
Directory.CreateDirectory(path);
Be careful that if textBox1.Text contains invalid path characters such as < > : then you'll get another exception.
Ok, so I'm new to C# and don't know what I'm doing wrong.
At the top of the button click event I have this variable:
InformationDump infodump;
infodump = new InformationDump();
After that I have the rest of the code which is meant to path to the user desktop and save whatever is filled out in the textbox's into a separate window (part of the same program):
infodump.richTextBox1.Text = textBox1.Text + ", " + textBox2.Text + ", " + textBox3.Text + ", " + textBox4.Text + ", " + comboBox1.SelectedItem;
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filename = path + "\\" + DateTime.Now.ToString("HH.mm.ss") + System.Environment.UserName + ".txt";
infodump.richTextBox1.SaveFile(filename, RichTextBoxStreamType.RichText);
Thanks in advance.
This is happening because you are creating an instance of the form (InformationDump) then try to use the RichTextBox instance but the form then will not go through the standard loading and initialization process therefore as I reproduced, the file will be created but will be empty.
It is very interesting, but if you do repeated save it works!
infodump.richTextBox1.SaveFile(filename, RichTextBoxStreamType.RichText);
// zero bytes
infodump.richTextBox1.SaveFile(filename, RichTextBoxStreamType.RichText);
// works!
Good afternoon. I have an asp.net web forms application using c#. I am having some difficulty getting my code to work properly. The data is uploaded successfully to the sql server database, but the file isn't saved to the specified "Data" folder. any help would be greatly appreciated?
The ID of the fileupload control is "fu_doc_upld". I don't get any errors when using the form, the files just aren't saving to the "Data" folder (or any other folder). Here is the code behind that I'm using:
protected void btn_frm_new_doc_save_close_Click(object sender, EventArgs e)
{
int i = 0;
string filename = fu_doc_upld.FileName;
if (fu_doc_upld.HasFile)
{
while (System.IO.File.Exists(Server.MapPath("~/Data/") + filename))
{
i++;
filename = fu_doc_upld.FileName + " (" + i.ToString() + ")";
fu_doc_upld.PostedFile.SaveAs(Server.MapPath("~/Data/") + filename);
}
}
hdn_filename_txt.Value = (Server.MapPath("~/Data/") + filename);
hdn_doc_uplod_dt_txt.Value = DateTime.Now.ToString();
SqlConnection idrf_cnxn = new SqlConnection("Data Source=WDBSVCPRD01\\SVCDB;Initial Catalog=idrf;Integrated Security=True");
{
SqlCommand new_doc_cmd = new SqlCommand("Insert Into tbl_doc(doc_title, doc_type_list, doc_org_list, doc_dept_list, doc_desc, prior_contract_cd, legal_comp_contract_id, doc_upld_dt, doc_path, vendor_id_fk) Values(LTRIM(RTRIM(#doc_title)), LTRIM(RTRIM(#doc_type_list)), LTRIM(RTRIM(#doc_org_list)), LTRIM(RTRIM(#doc_dept_list)), LTRIM(RTRIM(#doc_desc)), LTRIM(RTRIM(#prior_contract_cd)), LTRIM(RTRIM(#legal_comp_contract_id)), LTRIM(RTRIM(#doc_upld_dt)), LTRIM(RTRIM(#doc_path)), LTRIM(RTRIM(#vendor_id_fk)))", idrf_cnxn);
new_doc_cmd.Parameters.AddWithValue("#doc_title", doc_title_txt.Text);
new_doc_cmd.Parameters.AddWithValue("#doc_type_list", doc_type_ddl.Text);
new_doc_cmd.Parameters.AddWithValue("#doc_org_list", doc_org_ddl.Text);
new_doc_cmd.Parameters.AddWithValue("#doc_dept_list", doc_dept_ddl.Text);
new_doc_cmd.Parameters.AddWithValue("#doc_desc", doc_desc_txt.Text);
new_doc_cmd.Parameters.AddWithValue("#prior_contract_cd", prior_contract_cd_txt.Text);
new_doc_cmd.Parameters.AddWithValue("#legal_comp_contract_id", lgl_comp_cont_id_txt.Text);
new_doc_cmd.Parameters.AddWithValue("#doc_upld_dt", hdn_doc_uplod_dt_txt.Value);
new_doc_cmd.Parameters.AddWithValue("#doc_path", hdn_filename_txt.Value);
new_doc_cmd.Parameters.AddWithValue("#vendor_id_fk", hdn_vendor_id_txt.Value);
idrf_cnxn.Open();
new_doc_cmd.ExecuteNonQuery();
idrf_cnxn.Close();
if (IsPostBack)
{
Response.Redirect("~/Default.aspx");
}
}
}
Any help would be greatly appreciated.
Thanks,
J
You only call SaveAs() when the file exists. You could've found this by placing a breakpoint and stepping through your code.
Move the save out of the loop:
string filename = fu_doc_upld.FileName;
while (System.IO.File.Exists(Server.MapPath("~/Data/") + filename))
{
i++;
filename = fu_doc_upld.FileName + " (" + i.ToString() + ")";
}
fu_doc_upld.PostedFile.SaveAs(Server.MapPath("~/Data/") + filename);
Note that this code will remove the extension if a file already exists. See C#: How would you make a unique filename by adding a number? for a better implementation.
im using a file upload control and here is my code :
//Uploading the image
if (imageUpload.HasFile)
{
try
{
if (imageUpload.PostedFile.ContentType == "image/jpeg")
{
if (imageUpload.PostedFile.ContentLength < 102400)
{
string im = ( "~/User" + "/" + Page.User.Identity.Name + "/" + Page.User.Identity.Name + ".jpeg");
imageUpload.SaveAs(im);
uploadLabel.Text = "";
}
else
{
uploadLabel.Text = "File size must be less than 1024 kb";
}
}
else
{
uploadLabel.Text = "File must be in jpeg/jpg format";
}
}
catch(Exception ex)
{
uploadLabel.Text = "File upload failed becuase: " + ex.Message;
}
}
but im getting an error:
The SaveAs method is configured to require a rooted path, and the path "path" is not rooted.
what am i doing wrong.
thanks
SaveAs() requires an absolute path.
try using Request.PhysicalApplicationPath + "\\User"
The Save method is configured to require an absolute path (starting with X:\..., in some drive).
You should call Server.MapPath to get the absolute path on disk to ~/whatever.
Add Server.MapPath where you declare im
Server.MapPath gives you the absolute path.
string im = Server.MapPath("/User") + "/" + Page.User.Identity.Name + "/" + Page.User.Identity.Name + ".jpeg";
string filename = FileUpload1.FileName.ToString();
if (filename != "")
{
ImageName = FileUpload1.FileName.ToString();
ImagePath = Server.MapPath("Images");
SaveLocation = ImagePath + "\\" + ImageName;
SaveLocation1 = "~/Image/" + ImageName;
sl1 = "Images/" + ImageName;
FileUpload1.PostedFile.SaveAs(SaveLocation);
}
try this may be help ful.......