why asp.net mvc does not include the image uploaded folder in the project? - asp.net-mvc

what I have found that the images uploaded by my application are not included in the project and that I have to include them manually image by image so what is the correct way of asp.net mvc project to let the images uploaded to be included in the project.
The following code is the one that upload the images to the folder and creates a unique name for each image. but still those images are not included in the project explorer.
public ActionResult Create(Job job, HttpPostedFileBase JobImage)
{
var value = "999999999";
var result4 = from app in db.Job where app.UniqueJobImageName.Contains(value) select app;
if(result4.FirstOrDefault() != null)
{
value = generateRandom();
}
if (ModelState.IsValid && CheckFileType(JobImage.FileName))
{
string ext = Path.GetExtension(JobImage.FileName);
var fileName = Path.GetFileName(JobImage.FileName);
job.JobImage = JobImage.FileName;
job.UniqueJobImageName = value + ext;
var path = Path.Combine(Server.MapPath("~/Images"), value + ext);
JobImage.SaveAs(path);
job.UserId = User.Identity.GetUserId();
job.jobUrl = "";
job.Month = DateTime.Now.ToString("MMMM");
job.DateAndTime = DateTime.Now;
AllJobModel all = new AllJobModel
{
JobTitle = job.JobTitle,
JobDescription = job.JobDescription,
JobImage = job.JobImage,
UniqueJobImageName = job.UniqueJobImageName,
locationName = job.locationName,
minimumSalary = job.minimumSalary.ToString(),
maximumSalary = job.maximumSalary.ToString(),
jobUrl = job.jobUrl,
PostedDate = DateTime.Now.ToString("dd/MM/yyyy"),
UserId= User.Identity.GetUserId(),
};
db.AllJobModel.Add(all);
db.SaveChanges();
db.Job.Add(job);
db.SaveChanges();
return RedirectToAction("Index","Home");
}else if (!CheckFileType(JobImage.FileName))
{
}
return View(job);
}
enter image description here

This doesn't make any sense. The uploaded images are content created by the user. They are not a deployable component of the site (or, they shouldn't be anyway). They are user-generated content, not developer resources.
If you are looking to effectively migrate your data (i.e. copy database entries and other user-generated content such as image files) from one environment to another, then that is a separate task for which you can create a separate process and/or automated script. Don't confuse it with the job of uploading a new version of your application code.
P.S. Even if what you were asking for was a sensible goal, it's impossible anyway - the executable version of your code is not the same as the code you see in Visual Studio in your project. In a modern ASP.NET application the executable code is in a different folder (even when you're running in debug mode in Visual Studio) and it has no concept or knowledge of the original project it was compiled from, or where to find it, or how to interact with it.

Related

Random image from folder

How can I make possible that the app will load all of the images from the specific folder and then put in array and choose one image randomly? When chose one then pass to the fronted to show the image. How to do that too?
I am C# developer but not long time ago I found ElectronJS and this framework does everything easier so therefore I am moving to this framework.
I did in C# programming this way:
// basic settings.
var ext = new List<string> { ".jpg", ".gif", ".png" };
// we use same directory where program is.
string targetDirectory = Directory.GetCurrentDirectory() + "\\assets\\" + "images\\" + "animals\\";
// Here we create our list of files
// New list
// Use GetFiles to getfilenames
// Filter unwanted stuff away (like our program)
if (Directory.Exists(targetDirectory))
{
Files = new List<string>
(Directory.GetFiles(targetDirectory, "*.*", SearchOption.TopDirectoryOnly)
.Where(s => ext.Any(es => s.EndsWith(es))));
// Show first picture so we dont need wait 3 secs.
ChangePicture();
}
else
{
panel5.BackgroundImage = new Bitmap(Resources.doggy);
}
I don't know how to do in ElectronJS.
Thank you in advance the answers.
Alright. I found the solution.
However I don't understand the people who are giving negative reputation for the opened question. If they are giving negative reputation then they could explain why.
Well anyway, I did fix this issue with this way:
I created images.js file and added this:
var fs = require('fs');
function getRandImage() {
var files = fs.readdirSync('./assets/images/animals/')
/* now files is an Array of the name of the files in the folder and you can pick a random name inside of that array */
let chosenFile = files[Math.floor(Math.random() * files.length)]
console.log('../assets/images/animals/' + chosenFile);
return '../assets/images/animals/' + chosenFile;
}
module.exports = { getRandImage }
I used console to see if the value is correct, otherwise others can delete that part.
Sending the data to the renderer process:
const { getRandImage } = require('./images');
child.webContents.send('random-image', getRandImage());
I did put in the preload.js file the following (I used the starter pack electronjs github to start with something):
var { ipcRenderer } = require('electron');
ipcRenderer.on('random-image', function (event, store) {
document.getElementById("randompic").src = store;
console.log(store);
});
Same here, I did use console.log just for test the value is correct and I used to change the randompic ID related image src html to the randomly chosen image.
Hopefully I did helping those people who are newbie as me.

Azure DevOps Server 2019 Programmatially copy test case error Exception: 'TF237124: Work Item is not ready to save'."

I'm able to copy most test cases with this code (trying to copy shared steps to be part of the test case itself) but this one will not copy but I can not see any error message as to why - could anyone suggest anything else to try. See output from Immediate windows. Thanks John.
?targetTestCase.Error
null
?targetTestCase.InvalidProperties
Count = 0
?targetTestCase.IsDirty
true
?targetTestCase.State
"Ready"
?targetTestCase.Reason
"New"
foreach (ITestAction step in testSteps)
{
if (step is ITestStep)
{
ITestStep sourceStep = (ITestStep)step;
ITestStep targetStep = targetTestCase.CreateTestStep();
targetStep.Title = sourceStep.Title;
targetStep.Description = sourceStep.Description;
targetStep.ExpectedResult = sourceStep.ExpectedResult;
//Copy Attachments
if (sourceStep.Attachments.Count > 0)
{
string attachmentRootFolder = _tfsServiceUtilities.GetAttachmentsFolderPath();
string testCaseFolder = _tfsServiceUtilities.CreateDirectory(attachmentRootFolder, "TestCase_" + targetTestCase.Id);
//Unique folder path for test step
string TestStepAttachementFolder = _tfsServiceUtilities.CreateDirectory(testCaseFolder, "TestStep_" + sourceStep.Id);
using (var client = new WebClient())
{
client.UseDefaultCredentials = true;
foreach (ITestAttachment attachment in sourceStep.Attachments)
{
string attachmentPath = TestStepAttachementFolder + "\\" + attachment.Name;
client.DownloadFile(attachment.Uri, attachmentPath);
ITestAttachment newAttachment = targetTestCase.CreateAttachment(attachmentPath);
newAttachment.Comment = attachment.Comment;
targetStep.Attachments.Add(newAttachment);
}
}
}
targetTestCase.Actions.Add(targetStep);
targetTestCase.Save();
}
Since this code works for most test cases, this issue may come from the particular test case. In order to narrow down the issue, please try the following items:
Run the code on another client machine to see whether it works.
Try to modify this particular test case using the account API uses, to see whether it can be saved successfully.
Try validate the WorkItem prior to save. The validate() method will return an arraylist of invalid fields.

TFS API Pending Items, Exclude list, Include List

Background,
I am making a TFS merge tool to service out development and Branch requirements.
Withing this tool I have a business object layer that uses the Microsoft.Teamfoundation.ExtendedClient Package.
What I have
I currently have a function that does the checkin for my pending items
The 'changes' object has all my changes in it. Included and Excluded
Workspace workspace = _vcs.GetWorkspace(_workspaceName, _workspaceOwner);
WorkItemCheckinInfo checkInInfo = new WorkItemCheckinInfo(pbi, WorkItemCheckinAction.Associate);
PendingChange[] changes = workspace.GetPendingChanges();
ChangesetID = workspace.CheckIn(changes, checkInComment, null, new WorkItemCheckinInfo[] {
checkInInfo }, null);
What I need
I need to only get his list of 'Included' Pending changes.
Some have suggested using, bu this fails as ITS only has zero rows.
//get all candidate changes and promote them to included changes
PendingChange[] candidateChanges = null;
string serverPath = workspace.GetServerItemForLocalItem(_workspaceName);
List<ItemSpec> its = new List<ItemSpec>();
its.Add(new ItemSpec(serverPath, RecursionType.Full));
workspace.GetPendingChangesWithCandidates(its.ToArray(), true, out candidateChanges);
foreach (var change in candidateChanges)
{
if (change.IsAdd)
{
//ws.PendAdd(change.LocalItem);
}
else if (change.IsDelete)
{
//ws.PendDelete(change.LocalItem);
}
}
I have also tried this but SavedCheckin = null and i get an exception.
SavedCheckin savedCheckin = workspace.LastSavedCheckin;
// Create a list of pending changes.
var pendingAdds = new List<PendingChange>(workspace.GetPendingChanges());
List<PendingChange> excludedChanges = new List<PendingChange>();
for (int i = 0; i <= changes.Length - 1; i++)
{
if (savedCheckin.IsExcluded(changes[i].ServerItem))
{
excludedChanges.Add(changes[i]);
}
Console.WriteLine(changes[i].LocalItem.ToString() + " Change " + changes[i].ChangeType)
}
So I either need to iterate through the 'changes' list and remove 'Excluded' items
or there is bound to be something im missing here.
thanks in advance
Alan
There is no TFS API to get only Included changes, Included/Excluded changes sections exist in Visual Studio/Team Explorer. Visual Studio detects changes you make outside the system.
What you need is Visual Studio Extension API -- IPendingChangesExt Interface, you could refer to the article below or open a case on Visual Studio Extension side.
https://www.mztools.com/articles/2015/MZ2015007.aspx

Kentico 9 - Separate MVC Application - Document Attachment

I am trying to add an attachment to a document - I am running a separate MVC application - not part of the same solution as the Kentico admin site.
I am getting error site not found for my UserInfo.
public Models.PartnerUpdateModel NewPartnerUpdate(Models.PartnerUpdateModel partnerupdatemodel)
{
CMS.DocumentEngine.TreeNode newpartnerupdatetreenode = CMS.DocumentEngine.TreeNode.New(partnerupdatemodel.KenticoPartnerUpdate.ClassName);
TreeProvider tree = new TreeProvider();
partnerupdatemodel.KenticoPartnerUpdate.Title = partnerupdatemodel.Title;
partnerupdatemodel.KenticoPartnerUpdate.Summary = partnerupdatemodel.Summary;
partnerupdatemodel.KenticoPartnerUpdate.Article = partnerupdatemodel.Article;
partnerupdatemodel.KenticoPartnerUpdate.Author = partnerupdatemodel.MarketConnectUser.UserID.ToString();
partnerupdatemodel.KenticoPartnerUpdate.DocumentCulture = partnerupdatemodel.DocumentCulture;
partnerupdatemodel.KenticoPartnerUpdate.Insert(TreeHelper.SelectSingleNode(partnerupdatemodel.MarketConnectUser.DefaultGroup.GroupChildNodes.Where(x => x.Key == partnerupdatemodel.ParentClassName).Select(x => x.Value).Single()), true);
partnerupdatemodel.KenticoPartnerUpdate.SubmitChanges(true);
foreach (var file in partnerupdatemodel.Files)
{
HttpPostedFile postedfile = FileHelper.ConstructHttpPostedFile(FileHelper.ToByteArray(file.InputStream), file.FileName, file.ContentType);
DocumentHelper.AddAttachment(newpartnerupdatetreenode, Models.PartnerUpdateModel.AttachmentColumnNames.Image.ToString(), postedfile, new TreeProvider(MembershipContext.AuthenticatedUser)); //GETTING ERROR HERE
newpartnerupdatetreenode.Update();
}
return partnerupdatemodel;
}
MembershipContext is not oficially supported in v9 (Supported and unsupported Kentico features on MVC sites) however I think this should work - to be honest I have tried it by myself and I`ve got UserInfo (for public user and global admin too). Could you please provide call stack of exception?
edit: I have also tried to get current SiteInfo like
var site = SiteContext.CurrentSite;
and it`s null if you have not specified Presentation URL - could you check this setting?

Rotativa generating blank PDF in Server

We are using Rotativa in a .Net MVC 5 project. it works like a charm locally and fails (generates a blank PDF) equally when deployed to server. However, if I login to server and access the website as localhost it generates PDF just fine.
So it looked like a permission issue with which Application pool is running on. So, for testing purpose, I changed to run the App Pool to run on "Local System". Still same issue.
We have also tried:
"ViewasPDF"
App settings which looks like: <add key="WkhtmltopdfPath" value="<path to the folder>"/>
Below is the code:
return new ActionAsPdf("ActionMethod",new { id = id, partSelected = part, selectedTab = selectedTab, isDownload = true })
{
FileName = fileName,
PageMargins = { Left = 0, Right = 0 },
CustomSwitches = "--disable-external-links --disable-internal-links --disable-smart-shrinking --viewport-size 1600x900 --load-error-handling ignore",
PageOrientation = Rotativa.Options.Orientation.Portrait,
PageSize = Rotativa.Options.Size.A4,
PageWidth = 210,
PageHeight = 297
};
First, you have disabled displaying error with the custom switch --load-error-handling ignore. It might help to not have it disabled temporarily for the purpose of debugging.
Secondly, a blind guess from me is that you may have some images that require local path as src on the pdf? If that's the case you will need #Server.MapPath for each one of those.
For example:
<img src="#Server.MapPath("your image path")"/>
var iResult = new Rotativa.ActionAsPdf("PrintForm", new { Id = Id }) { FileName = iFilename, SaveOnServerPath = ViewBag.FileName };
iResult.UserName = System.Configuration.ConfigurationManager.AppSettings["ADUserID"].ToString();
iResult.Password = System.Configuration.ConfigurationManager.AppSettings["ADPassword"].ToString();
return iResult;
Here is a code example where aduser is user name and password, please create a generic user account for this..

Resources