C# : upload file to a file host - c#

i have been trying to upload a file to Fileshawk.com from my application.
Here is how i did it :
1- Created a web browser control
2- Loaded the page
3- in the web page it has an input tag of file.
4- i tried to add the file to HTML by using that method which failed.
private void Set_Text_TAG_INPUT(string attribute, string attname, string value)
{
// Get a collection of all the tags with name "input";
HtmlElementCollection htmle = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement current in htmle)
{
if (current.GetAttribute(attribute).Equals(attname))
{
current.SetAttribute("value", value);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Set_Text_TAG_INPUT("id","upfile_1342028154587", "FILE.txt");
}
So it didn`t change the value of the Input tag.
Is there any way to add my file to the HTML or page code and about pressing upload i already have a method that invokes the web browser button and click it.
For example , when you click Select file from the input file tag , a window pop up and you select the file , now when you click okay , where is that file stored in the HTML ?
Finally excuse me if i am a newbie in HTML and web knowledge.

This is a security feature in browsers. You cannot programmatically set the value of the value of an input tag of type 'file' since it would introduce a security hole (e.g. a malicious developer could set the value right before handling a submit event and get whatever file they wanted).

Use WebScraper, a C# .NET library to upload file/download file easily, but you need to analyze Fileshawk.com HTML to come up with your own WebScraper syntax, there are totally 22 file host syntax available for sample and already useable.
http://sorainnosia.com/Home/Article/WEBSCRAPER

Related

How to open url in new browser tab from Web browser control (displaying PDF having a link) c#

I am facing this issue, working on Active reports 9. Every thing is fine as per our application we generate the report and in UI user will be viewing in a c# Web browser control.
Now the issue am facing is when client(user) clicks on the link present in pdf i.e. on Web Browser control. With in the same window the link is opening. They want the link to open in new window.
The problem q=am facing is if its Html control i would have used target="_blank" property but not , and its a windows application i cant even use Java script. I just gone through the properties of Picture control used in Report, theres only Hyperlink property which states in pdf it converts it to href or a tag.
Need some assistance as soon as possible is that possible to do in web browser control or should change any properties for picture control in Code behind.
Hope this help you. It worked for me.
Add the Navigating event on your webBrowser control. This will open the link in a new Browser window. In my case Google Chrome.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
try
{
if (!(e.Url.ToString().ToLower().Contains("file") || e.Url.ToString().ToLower().Contains("pdf")))
{
e.Cancel = true;
//Open Link
Process.Start(e.Url.ToString());
}
}
catch (Exception err)
{
//Handle Exception
}
}

how can I upload a file via file input box using webbrowser in C#

May be this question is asked in past but I have searched and not found its solution yet.
I have tried all the options that I have found till now but all in vain.
SendKeys doesn't work as it does not fill the file input box with file path, that is to be uploaded.
Cannot set file input box "SetAttribute" value as there is no value attribute available:
thats all.
If I use element.focus() it pops up "choose file to upload" dialog and now I don't know how to fill it programmatically and open it in file input box.
I want it to be automated completed so that user does not have to interact with the application.
Application shall pick the file from hard disk from given file path and fill other fields of form then start uploading, all using webbrowser control in windows form application.
No solutions found!
Can anyone help please? (This is my first ever question on stackoverflow, therefore if I am doing anything wrong then please guide, I mean if I am not allowed to post such question!)
Here is the code:
HtmlElementCollection heCollection = doc.GetElementsByTagName("input");
foreach (HtmlElement heSpan in heCollection)
{
string strType = heSpan.GetAttribute("type");
string strName = heSpan.GetAttribute("name");
if (strType.Equals("file") && strName.Equals("file"))
{
heSpan.Focus();
//heSpan.SetAttribute("value", "test.jpg");
SendKeys.Send("C:\\1.txt");
//heSpan.InnerText = "c:\\1.txt";
}
//Title for the attachment
if (strName.Equals("field_title"))
{
heSpan.InnerText = "1.txt";
}
}
When this code executes, cursor starts blinking in fine input box (as I have set heSpan.focus()) but the file path doesn't show in the file input box.
If I implement
heSpan.InvokeMember("click");
It opens the choose a file to upload dialoge/popup window and there I get stuck, because I don't know how to fill that popup dynamically and then insert the file path in file input box.
Try setting the focus to the WebBrowser control right before you set the focus to the input field.
That worked for me.

Image Preview from on click event c# asp .net

I'm currently trying to look at a directory, and then preview a .jpeg from a list box. I have the list box populating with the contents of the directory and only showing Jpegs, but I can't think of what to do to get the jpeg preview in a picture box. I'm using an asp .net application on Visual Studio 2010.
This is the code I have
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo infoDir = new DirectoryInfo(#"G:\Test_Directory");
FileInfo[] infoFile = infoDir.GetFiles("*.jpeg");
foreach( FileInfo file in infoFile )
{
lstDirectory.Items.Add(file.Name);
}
}
protected void lstDirectory_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
I'm under the understanding Postback needs to be used. If anyone is able to help, that would be great.
The file which is in the G: Drive, is a jpeg, which can be seen in the list box is : jpegimage.jpeg
Thanks.
How about something like this?
I think you could do this mostly in Javascript, with two additional ASP.NET page.
First, create a new web page. We'll call this A.aspx. This page will be passed the image name in the query string. It will be very simple: it will just fetch the contents of the file from "G:\TestDirectory" and write it to the Response stream. There are quite a few questions and answers on Stack Overflow on how to do this, if you haven't done it before.
Then, create another web page. We'll call this B.aspx. This will have an image control with height and width set appropriately. It will also take the image name from its query string. The code-behind will build a URL to use as the ImageSource property on the image control. The URL will be that of A.aspx, with the (URL-encoded) image name appended as a parameter.
On your ASP.NET page, hook up an event handler to your listbox. When the selected index on the list box changes, on the client side, build a URL, based on the URL to B.aspx with the image name from the list box appended as a parameter. Then open a window, using the URL you just built, pointing to B and passing the desired file name.
So: when the list box selected index changes (or when you double click, or whatever event you pick), the javascript will open a window with page B.aspx. Page B will have an image control, set to the URL to A.aspx. A.aspx will stream the image contents to the image control, which will appear in your new window.

Web Browser control click

I am using Web Browser control in my project and i open a web that have file chooser object.
Now i want to inject programmatically a path to a file in this file chooser object .
I tried to get the HtmlElement ,but i not found in the source code the element Id.
Edit:
In the web there is "Browse..." button that open a file chooser,and then the file path is show in little text filed, and i want to inject the file path my self to the text field.
First.
establish a name, id or class for the element you want to change the value of. There are a number of ways of doing this. I would use firebug for firefox, or IE developer toolbar, or just view the source of the page and establish what the name/Id of the field is. Now if that field does not have a Name or an ID that you can use you might be able to get the containing element and iterate through the Child elements n times until you get the element you want.
For my embedded browser app I did the following in c#:-
// webBrowser is the name of the embedded IE browser in your app
var htmlDocument = webBrowser.Document;
if(htmlDocment!=null)
{
var field = htmlDocument.GetElementById("...the id...");
if(field!=null)
{
field.SetAttribute("value","...yourfilenamepathonyourmachine...");
}
// Now you would need to establish the ID of the submit element and click that
var submitButton = htmlDocument.GetElementById("...submit button...");
if(submitButton!=null)
{
submitButton.InvokeMember("Click");
}
// your code to loop?
}
So do you have the HTML of the page in question? that might help and a better answer can be given
Regards Julian

Modifying rich content on home page programmatically

I am provisioning site collections programmatically in SP2010. The process is working fine but I need to be able to customize some text on the home page that is contained in rich text (not in a web part). Is there a way to grab that HTML code and modify it from code? For instance, embedded within the home page, I have a title and a paragraph of text. I'd like to customize the title based on one of the input values provided from the provisioning request. Is it possible to do this? Any suggestions would be appreciated!
You can access the content area as follows:
PublishingWeb pw = null;
//"web" is your SPWeb, didn't include using statement for your SPSite/SPWeb in this sample
if (PublishingWeb.IsPublishingWeb(web))
{
pw = PublishingWeb.GetPublishingWeb(web);
}
//todo: add some handling here for if web is not a publishingWeb
//assuming your home page is the default, if not - get the correct page here
SPFile defaultpage = pw.DefaultPage;
if (!(defaultpage.CheckOutType == SPFile.SPCheckOutType.None))
{
//programatically creating this stuff, so cancel any unexpected checkouts
defaultpage.UndoCheckOut();
}
defaultpage.CheckOut();
//Field you want to add HTML in (Alternatively, retreive existing data and modify the HTML)
defaultpage.ListItemAllFields["PublishingPageContent"] = "string of HTML";
defaultpage.ListItemAllFields.Update();
defaultpage.CheckIn("My Comment");

Categories