C# - Check URL is valid for TemplateString - c#

I'm currently using a TemplateString to load an image with a link, where {Url} is the file name obtained from the database.
Here is the code I use, as a reference:
Ext.Net.TemplateColumn TheColumn = new Ext.Net.TemplateColumn();
TheColumn.Text = "Image";
TheColumn.DataIndex = "Url";
TheColumn.Align = Alignment.Center;
TheColumn.Flex = 1;
TheColumn.MinWidth = 70;
TheColumn.TemplateString = "<a href='http://example.com/{Url}' data-lightbox='{Url}'><img style='width:60px;height:60px;' src='http://example.com/{Url}'/></a>";
What I would like to do is that I could check if the requested image exists, and if it doesn't, I use an alternative TemplateString instead.
Since I'm using jQuery, I´m leaving the tag in case. The project is an ASP.NET website.

Related

Get XML String in PHP with URL

I'm really new to the job as a web developer and I have a Problem that I cant solve alone, also I can't find any answers which fit my Problem.
So this is the Construct, I have a PHP Page with this Code:
<?php
$url = 'http://myserver.de/list.aspx';
$xml = new SimpleXMLElement($url);
$name = $xml->List->member->name;
?>
And I got this C#-Code from an aspx-project (list.aspx):
StringBuilder xmlBuilder = new StringBuilder();
xmlBuilder.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
xmlBuilder.Append("<List>");
foreach (var nameandplz in fullname.Zip(plz, Tuple.Create))
{
xmlBuilder.Append("<member>");
xmlBuilder.Append("<name>" + nameandplz.Item1 + "</name>");
xmlBuilder.Append("<postcalcode>" + nameandplz.Item2 + "</postalcode>");
xmlBuilder.Append("</member>");
}
xmlBuilder.Append("</List>");
Context.Response.ContentType = "text/xml";
Context.Response.BinaryWrite(Encoding.UTF8.GetBytes(xmlBuilder.ToString()));
Context.Response.End();
So I just use the StringBuilder to build a String with XML.
fullname and plz are listed.
In my PHP-CIde i call the URL and the Problem is that the SimpleXMLElement doesnt write the XML-Code into the $xml.
I tried everything, i called the URL manually and the XML File is displayed correctly.
Do I use the wrong format to get the XML File per SimpleXMLElement?
To get the data from the URL you must set $data_is_url = true according to the PHP Documentation ( http://php.net/manual/fr/simplexmlelement.construct.php ); so XMLElement try to build an XML from the string given.
You can use the function libxml_get_errors to get XML errors.
So here a code that will get the content from the URL :
<?php
$url = 'http://myserver.de/list.aspx';
$xml = new SimpleXMLElement($url, 0, true);
$name = $xml->List->member->name;
Caution : you access List / Member / Name without testing if List Member or Name exists.

Moving files with Google Drive API v3

Im trying to move a file from one folder to another using the Google Drive API v3. I found documentation how to this here. I used the .NET sample code from the documentation page and created a method that looks like this:
public ActionResult MoveFile(string fileToMove, string destination)
{
DriveService service = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = <USER CREDENTIAL>,
ApplicationName = "APPNAME"
});
var searchFiles = service.Files.List();
searchFiles.Corpus = FilesResource.ListRequest.CorpusEnum.User;
searchFiles.Q = "name = '" + fileToMove + "'";
searchFiles.Fields = "files(*)";
string fileToMoveId = searchFiles.Execute().Files[0].Id;
searchFiles.Q = "name = '" + destination + "'";
string destinationId = searchFiles.Execute().Files[0].Id;
//Code used from documentation
// Retrieve the existing parents to remove
var getRequest = service.Files.Get(fileToMoveId);
getRequest.Fields = "parents";
var file = getRequest.Execute();
var previousParents = String.Join(",", file.Parents);
// Move the file to the new folder
var updateRequest = service.Files.Update(file, fileToMoveId);
updateRequest.Fields = "id, parents";
updateRequest.AddParents = destinationId;
updateRequest.RemoveParents = previousParents;
file = updateRequest.Execute();
return RedirectToAction("Files", new {folderId = destinationId});
}
When I execute this code I get the following error:
The parents field is not directly writable in update requests. Use the
addParents and removeParents parameters instead.
The error doesn't really makes sense to me because this code sample came from the documentation page itself. I can't figure out what other paramters they mean. What addParents and removeParents parameters do they mean? Are updateRequest.AddParents and updateRequest.RemoveParents not the right parameters?
Ok here is the problem.
var updateRequest = service.Files.Update(file, fileToMoveId);
The method is requiring that you send a body of a file to be updated. This normally makes sense as any changes you want to make you can add to the body.
Now the problem you are having is that you got your file from a file.get. Which is totally normal. This is how you should be doing it. THe problem is there are some fields in that file that you cant update. So by sending the full file the API is rejecting your update. If you check Files: update under Request body you will see which fiends are updateable.
Issue:
Now this is either a problem with the client library or the API I am going to have to track down a few people at Google to see which is the case.
Fix:
I did some testing and sending an empty file object as the body works just fine. The file is moved.
var updateRequest = service.Files.Update(new Google.Apis.Drive.v3.Data.File(), fileToMove.Id);
updateRequest.AddParents = directoryToMove.Id;
updateRequest.RemoveParents = fileToMove.Parents[0];
var movedFile = updateRequest.Execute();
This method works well when working in your own drive, but not in a team drive where a file (folder) can only have 1 parent strictly. I do not have the solution in a team drive

Rally C#: Is it possible to create a new user story along with an attachment? (Avoiding to query for the user-story reference)

Please be patient I have already gone through the example from: How to Add an Attachment to a User Story using Rally REST .NET
However the sample code requires a query to identify the newest user story reference. The sample illustrates how to add an attachment to an existing user story. I would like to accomplish: Creating a new user story along with an attachment.
Here is my method.
public void createUsWithAttachment(string workspace, string project,
string userStoryName, string userStoryDescription){
//Authenticate with Rally
this.EnsureRallyIsAuthenticated();
//UserStory Setup
DynamicJsonObject toCreate = new DynamicJsonObject();
toCreate[RallyField.workSpace] = workspace;
toCreate[RallyField.project] = project;
toCreate[RallyField.name] = userStoryName;
toCreate[RallyField.description] = userStoryDescription;
//get the image reference - assume that this is where the image lives after being downloaded from Outlook
String imageFilePath = "C:\\Users\\secret\\...";
String imageFileName = "file.png";
String fullImageFile = imageFilePath + imageFileName;
Image myImage = Image.FromFile(fullImageFile);
// Convert Image to Base64 format
string imageBase64String = imageToBase64(myImage, System.Drawing.Imaging.ImageFormat.Png);
// Length calculated from Base64String converted back
var imageNumberBytes = Convert.FromBase64String(imageBase64String).Length;
// DynamicJSONObject for AttachmentContent
DynamicJsonObject myAttachmentContent = new DynamicJsonObject();
myAttachmentContent["Content"] = imageBase64String;
try
{
//create user story
CreateResult createUserStory = _api.Create(RallyField.hierarchicalRequirement, toCreate);
//create attachment
CreateResult myAttachmentContentCreateResult = _api.Create("AttachmentContent", myAttachmentContent);
String myAttachmentContentRef = myAttachmentContentCreateResult.Reference;
Console.WriteLine("Created: " + myAttachmentContentRef);
// DynamicJSONObject for Attachment Container
DynamicJsonObject myAttachment = new DynamicJsonObject();
//Note the below commented line
/*myAttachment["Artifact"] = ;*/
myAttachment["Content"] = myAttachmentContentRef;
myAttachment["Name"] = "AttachmentFromREST.png";
myAttachment["Description"] = "Attachment Desc";
myAttachment["ContentType"] = "image/png";
myAttachment["Size"] = imageNumberBytes;
//create attachment
CreateResult myAttachmentCreateResult = _api.Create("Attachment", myAttachment);
}
catch (WebException e)
{
Console.WriteLine(e.Message);
}
}
In the above commented line I need to get a reference to the created user story which I can do, but that would require another method to fetch the user story which I have.
However I am wondering if this can be accomplished a different way similar to how we can create a user story with the dynamicJsonObject staging.
I was thinking something like this would work, but I am having a tough time.
DynamicJsonObject toCreate = new DynamicJsonObject();
toCreate[RallyField.workSpace] = workspace;
toCreate[RallyField.project] = project;
toCreate[RallyField.name] = userStoryName;
toCreate[RallyField.description] = userStoryDescription;
toCreate["Content"] = imageToBase64;
CreateResult createUserStory = _api.Create(RallyField.hierarchicalRequirement, toCreate);
My above assumption is not going as planned and I was wondering if there is a way to create a new user story with an attachment, without having to query for the userstory reference as neatly illustrated in the example from the link provided.
Thanks and credit to the author from the example in the link.
You should already have what you need to associate it from the create result:
myAttachment["Artifact"] = createUserStory.Reference;

Convert objects to an html file in asp.net mvc

I am trying to convert objects into an html file that resides in a folder on server. The current code creates objects and adds input from the user into the objects.
var loanInfo = new LoanInformation();
loanInfo.Purpose = "Refinance";
loanInfo.RefinanceReason = model.RefinanceDetails.RefiReason;
loanInfo.CurrentLender = model.RefinanceDetails.CurrentLender;
loanInfo.Year = model.RefinanceDetails.VehicleYear;
loanInfo.Make = model.RefinanceDetails.VehicleMake;
loanInfo.Model = model.RefinanceDetails.VehicleModel;
loanInfo.Vin = model.RefinanceDetails.Vin;
loanInfo.DesiredAmount = model.RefinanceDetails.DesiredLoanAmt;
loanInfo.DesiredPayment = model.RefinanceDetails.DesiredLoanPayment;
loanInfo.DesiredTerm = model.RefinanceDetails.DesiredTerm;
loanInfo.Mileage = model.RefinanceDetails.VehicleMileage;
Then from there it is bundled in this code
var id = _Interface.CreateLoanApplication(model.LoanType, primaryApplicant, jointApplicant,
primaryEmployment, jointEmployment, loanInfo, qieNotes);
I need to take the id variable and generate an HTML file with it. It currently is sent off to a service, but I am stuck as to how to stick it into an html file on server.

Publishing stream appears with no images - Facebook

I am building a small facebook application using the Facebook developer toolkit 3.0 in c#. I had this piece of code that used to work a week ago. From some reason, when I try publishing my feed it appears without an image and without a Flash movie. Here is my code:
string userName = FacebookAPI.Users.GetInfo().name;
string message = "";
attachment attach = new attachment();
attach.href = string.Format(#"http://apps.facebook.com/{0}/?uId={1}&uName={2}", Consts.APP_NAME, ViewerUserId, userName);
attachment_media_flash flash = new attachment_media_flash();
flash.type = attachment_media_type.flash;
flash.expanded_width = 320;
flash.expanded_height = 260;
flash.height = 100;
flash.width = 130;
message = "";
attach.name = ""
attach.caption = "this works";
attach.description = ""
flash.imgsrc = string.Format(#"{0}/flash/Pinguin.JPG", Consts.DOMAIN_NAME);
flash.swfsrc = string.Format(#"{0}/flash/pinguin.swf", Consts.DOMAIN_NAME);
List<attachment_media> attach_media_list = new List<attachment_media>();
attach_media_list.Add(flash);
attach.media = attach_media_list;
/* action links */
List<action_link> actionlink = new List<action_link>();
action_link al1 = new action_link();
al1.href = string.Format(#"http://apps.facebook.com/{0}", Consts.APP_NAME);
al1.text = "myApp";
actionlink.Add(al1);
FacebookAPI.Stream.Publish(message, attach, actionlink, null , Convert.ToInt64 (ViewerUserId));
Make sure that:
1: The URL you pass is not the one that of the FB app, like http://apps.fb.co/whatever/image.jpg;
it should be your domain URL, like http://www.example.com/images/myimage.jpg.
2: You need to pass the full URL of the image including the protocol (http:// / https://), like this:
http://www.example.com/images/myimage.jpg
The following is wrong:
www.example.com/images/myimage.jpg

Categories