Skip to content

Instantly share code, notes, and snippets.

View jens1101's full-sized avatar

Jens Tschirpig jens1101

View GitHub Profile
@jens1101
jens1101 / fileParse.js
Created June 20, 2016 13:26
Parsing a text file as JSON in JavaScript
function parseFile() {
var fileInput = document.createElement('input');
fileInput.setAttribute('type', 'file');
fileInput.onchange = function () {
var file = fileInput.files[0];
var reader = new FileReader();
reader.onload = function () {
console.log(JSON.parse(reader.result))
};
@jens1101
jens1101 / abilityScore.js
Last active July 28, 2016 09:15
Standard method to get an ability score. Droll 4d6, drop the lowest and sum the rest.
function ability() {
let ability = [];
let lowestIndex = 0;
let lowestScore = 6;
for (let i = 0; i < 4; i++) {
let score = (Math.floor((Math.random() * 6) + 1));
ability.push(score);
if(score < lowestScore) {
lowestScore = score;
@jens1101
jens1101 / AddFile.cs
Last active July 4, 2016 11:31
Adding files to ZIP in C#
function saveFileAsZip(string filePath) {
using (ZipFile zip = new ZipFile())
{
//Add string
zip.AddEntry("dataset.json", "{ foo: 'bar' }");
//Add file as byte array
zip.AddEntry("customName.json", System.IO.File.ReadAllBytes(filePath));
//Add file, preserving path
zip.AddFile(filePath);
@jens1101
jens1101 / angular-upload.js
Last active May 22, 2018 09:04
Upload files via AJAX
angular.module('uploadFile', []).service('uploadService', ['$http', function ($http) {
this.uploadFile = function () {
var field = document.createElement('input')
field.setAttribute('type', 'file')
field.setAttribute('accept', '.json, application/json')
field.onchange = function () {
var data = new FormData()
data.append('connectionId', 'banana')
data.append('fileToImport', field.files[0])
@jens1101
jens1101 / SomeController.cs
Created July 13, 2016 15:15
ASP.NET MVC return custom response
if(fileToImport.ContentLength > 5 * 1024 * 1024)
{
Response.StatusCode = 500;
Response.Write("File is too large to import! The max file size is 5MB.");
Response.StatusDescription = "Internal server error";
return;
}
@jens1101
jens1101 / gzip.cs
Created July 28, 2016 08:46
Adding files to Gzip in C#
public static void Compress(FileInfo fileToCompress)
{
var bytes = File.ReadAllBytes(fileToCompress.FullName);
using (FileStream fs = new FileStream(fileToCompress.FullName + ".gzip", FileMode.CreateNew))
using (GZipStream zipStream = new GZipStream(fs, CompressionMode.Compress, false))
{
zipStream.Write(bytes, 0, bytes.Length);
}
}
@jens1101
jens1101 / arrayCopy-es5.js
Created July 28, 2016 09:03
Copy contents of one array into another without breaking references in JS
function arrayCopy(to, from) {
Array.prototype.push.apply(to, from);
}
@jens1101
jens1101 / index.html
Created October 11, 2016 11:48
Set iframe contents via jQuery
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<iframe></iframe>
@jens1101
jens1101 / app.js
Created October 18, 2016 08:56
Angularjs basic transclusion with require
angular.module('testApp', [])
.component('parent', {
template: '<h2>{{$ctrl.title}}</h2><div ng-transclude></div>',
transclude: true,
controller: function() {
this.title = 'Foobar'
}
})
.component('child', {
require: {
@jens1101
jens1101 / controller.php
Created October 20, 2016 07:15
Posting data to Codeigniter via pure Angularjs
<?php
class User
{
public function save()
{
$username = $this->input->post('name');
$userData = json_decode($this->input->post('data'));
$doStuff($username, $userData);
}