Check extension of choosing picture in SP 2013 Picture Library - c#

I need to check pictures extension when user has chosen picture and try to upload it into picture library.
I've found the way to edit master page with js script, but i haven't to edit master page. Then i try to use event receiver Adding, but it can't get the name of file or file path. I used:
var file = properties.ListItem.File.Name; //properties.ListItem - returns null
AfterProperties also returns null.
The another way i see is to edit Adding Picture form with js:
I think is's simplest way but i can't found information about it.
Problem: how to set js script to the form (see picture) or how to do such actions with another way

It can be realized with event receiver ItemAdding and with
properties.AfterUrl
The same answered question with code in:
Customizing upload file functionality in SharePoint picture library

In your case, a simple JS form validation will do.
First I would prevent default action of the submit button.
Then, I would analyze the contents of the filename textbox and check the extention. If it's not one of the listed in the validation script, return false + alert "THis file extention is not supported".
Why would you want to use event receiver? It catches the event on server side.

Related

Selenium - Uploading image to website when input doesn't exist in HTML code and web uses SelectFileDialog

I have this problem for I can't find proper solution.
On website trovo.live I'm trying to change profile picture with code. There is 'Upload' button, which opens SelectFileDialog.
When I try to find input tag via document.getElementsByTagName("input") it doesn't find any input for uploading. (So I can't just do element.SendKeys() )
It works this way, when I select file in dialog then website converts it into data URI and set it as < img > src + it sets style = transform: matrix(values params)
So I found this solution that I convert image into data URI and set it as src parameter like website do it. Also I calculate matrix parameter values.
My problem is when I click save button later, website for some reason resets my css style and then picture is saved with bad positioning
After I change src by code (transform style remains from previous avatar)
This is how it looks when I upload manualy by Upload button or I can matrix() values
But when I click save button, it saves like that first picture, like it wouldn't count in my matrix() values. Can I somehow get into save button function and force in my matrix() edit? Or simulate Upload function without pressing button?
Or how can I handle selectFileDialog in selenium when
No < input > in HTML code
I can't use System.Windows.Forms.SendKeys.SendWait(path);
I also tried to use AutoItX, but I had some problems with their library in my project and I would like to handle it without libraries if possible.
Thank you very much for all answers :)
**edit
I found .js file which contains code for it, but I have no idea how to call for example method for resizing on method image code for javascript

asp.net FileUpload 'open' click to copy title

I am using a asp.net FileUpload control to upload files. There is a 'Title' text box which allows user to enter file title as well. The 'Upload' button then uploads the file to the server. This is all working fine. My problem however is that, customer has asked to copy the file name automatically to the title 'text box' in case user wants the custom title to be the same as file name.
Unfortunately, I am unable to figure this one out. I thought there would be an event behind the 'open' button of the file upload, that I could tap into and just as the title gets displayed on the fileupload control, similarly it should also get displayed on my title text box field. Perhaps Javascript/JQuery might help.
Any help in the right direction would be greatly appreciated. Many thanks
You can use the input's change event with jQuery:
$(function () {
$('input:file').on('change', function () {
console.log($(this).val());
})
})
You will probably have to parse the value to remove the "fakepath" stuff that some browsers add.
You can do this using the split method:
var title = $(this).val().split('\\');
console.log(title[title.length - 1])

How to stop users from manually editing file path in HTML file input control?

I have a asp.net form with 5 HTML file input controls with runat=server and a submit button. after user selects the files and clicks the submit button, the files are upload in the server.
Problem is the HTML file input controls are editable, and user can edit the path after he has already selected the file from the browse button.
If he enters a invalid file path, the file is not uploaded because it does not exist.
How can I stop users from manually changing the file path? I have tried to make the controls read only, but it disables the browse button also.
You can't. Write your server side logic to cope with missing uploads.
you could try something like having a hidden field and when the user selects the file from browse, it populates to the hidden field as well. Then when they upload, you either read from the hidden field for your upload or using JS replace the values back to the original control and read from that.
Is this what you want to have...
You can do like that hiding the control when user select the file
and showing only the path to the user
in a div
if he disagree, let him click on a link called Choose Another next to the path div, so when user click Choose another toggle the same div with flushing the previous value
see this fiddle : http://jsfiddle.net/T2D3X/
While brian's suggestion works (Thank you brian for this), finally we decided to follow the server side handling approach. since in case of invalid file, the file size would be zero, so the response is instant and we do not need to wait till the file is uploaded because in this case there is not file in the first place.
//check if file exists
if (uploadCtrl.PostedFile.ContentLength > 0)
{
uploadCtrl.PostedFile.SaveAs(fileNameWithPath);
uploadCtrl.Dispose();
}
else
{
fileNameWithPath = "invalid";
}
You can also use these methods:
One before validating: http://jsfiddle.net/T2D3X/1/
One After validating: http://jsfiddle.net/T2D3X/2/
Friend of mine posted the same answer before I have re-modified it.
There are some internal jsfiddle error on second link you can ignore that, i have just added more functionally to check on submit what value exactly you are sending.
Let us know if you find the it helpful ;)
Thank!

Click Gmail Refresh button

I am writing a simple personal app that has a browser control and I want it to automatically "Refresh" gmail to check it more often than it does by default. There are monkey scripts that do this but I'm trying to add my personal style to it.
Anyhow, I've looked around and found everything but what I can do in csharp using the browser control.
I found this:
// Link the ID from the web form to the Button var
theButton = webBrowser_Gmail.Document.GetElementById("Refresh");
// Now do the actual click.
theButton.InvokeMember("click");
But it comes back with null in 'theButton' so it doesn't invoke anything.
Anyone have any suggestions?
It's been awhile since I've used JavaScript, but given the other answers and comments that there is no real ID associated with the element, could you do something like the following:
Search all Div's with an attribute of Role == 'Button' and an InnerHtml == 'Refresh'.
Once the correct InnerHtml is found, get the Element.
Invoke the click on the found Element.
Again, this may be blowing smoke, but thought I'd throw it out there.
edit: Just realized you are doing this with C# and a browser control; however, the concept would still be the same.
The best suggestion I could give you at this point involves an existing API that is used for .NET web browser based automation:
http://watin.org/
Since the div tag with the desired button really only seems to identify itself with the class name, you could use the Find.BySelector(“”) code included with the most recent version of watin.

URL and Query management Asp.Net C#

Ok so while back I asked question Beginner ASP.net question handling url link
I wanted to handle case like this www.blah.com/blah.aspx?day=12&flow=true
I got my answer string r_flag = Request.QueryString["day"];
Then what I did is placed a code in Page_Load()
that basically takes these parameters and if they are not NULL, meaning that they were part of URL.
I filter results based on these parameters.
It works GREAT, happy times.... Except it does not work anymore once you try to go to the link using some other filter.
I have drop down box that allows you to select filters.
I have a button that once clicked should update these selections.
The problem is that Page_Load is called prior to Button_Clicked function and therefore I stay on the same page.
Any ideas how to handle this case.
Once again in case above was confusing.
So I can control behavior of my website by using URL, which I parse in Page_Load()
and using controls that are on the page.
If there is no query in URL it works great (controls) if there is it overrides controls.
Essentially I am trying to find a way how to ignore parsing of url when requests comes from clicking Generate button on the page.
Maybe you can put your querystring parsing code into IsPostBack control if Generate button is the control that only postbacks at your page.
if (!IsPostBack)
{
string r_flag = Request.QueryString["day"];
}
As an alternative way, at client side you can set a hidden field whenever user clicks the Generate button, then you can get it's value to determine if the user clicked the Generate button and then put your logic there.

Categories

Resources