I'm using Plupload to manage large file uploads. I have written a C# handler to upload using FTPWebRequest and it appears to work ok. The trouble is I would like to use chunking (http://www.plupload.com/docs/Chunking). When i enable chunking, I get multiple files uploaded to my FTP server called blobxxx, blobyyyy. The question is, how do I join them all together at the end? Here is my code - thanks for any help:
Plupload.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="plupload.aspx.cs" Inherits="plupload" %>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Plupload - Custom example</title>
<!-- production -->
<link href="plupload/js/jquery.plupload.queue/css/jquery.plupload.queue.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js" charset="UTF-8"></script>
<script type="text/javascript" src="plupload/js/plupload.full.min.js"></script>
<script src="plupload/js/jquery.plupload.queue/jquery.plupload.queue.min.js" type="text/javascript"></script>
<!-- debug
<script type="text/javascript" src="../js/moxie.js"></script>
<script type="text/javascript" src="../js/plupload.dev.js"></script>
-->
</head>
<body style="font: 13px Verdana; background: #eee; color: #333">
<h1>Custom example</h1>
<p>Shows you how to use the core plupload API.</p>
<div id="filelist">Your browser doesn't have Flash, Silverlight or HTML5 support.</div>
<br />
<div id="uploader">
<p>Your browser doesn't have Flash, Silverlight or HTML5 support.</p>
</div>
<script type="text/javascript">
// Initialize the widget when the DOM is ready
$(function() {
// Setup html5 version
$("#uploader").pluploadQueue({
// General settings
runtimes : 'html5,flash,silverlight,html4',
url : "FileUpload.ashx",
chunk_size: '1mb',
max_retries: 3,
rename : true,
dragdrop: true,
// Resize images on clientside if we can
resize: {
width : 200,
height : 200,
quality : 90,
crop: true // crop to exact dimensions
},
// Flash settings
flash_swf_url : 'plupload/js/Moxie.swf',
// Silverlight settings
silverlight_xap_url : 'plupload/js/Moxie.xap'
});
$("#uploader").bind("Error", function (upload, error) {
alert(error.message);
});
// only allow 5 files to be uploaded at once
$("#uploader").bind("FilesAdded", function (up, filesToBeAdded) {
if (up.files.length > 5) {
up.files.splice(4, up.files.length - 5);
showStatus("Only 5 files max are allowed per upload. Extra files removed.", 3000, true);
return false;
}
return true;
});
});
</script>
</body>
</html>
FileUpload.ashx
<%# WebHandler Language="C#" Class="FileUpload" %>
using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
public class FileUpload : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
for (int a = 0; a <= context.Request.Files.Count - 1; a++)
{
FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(new Uri("ftp://xxxx/yyyy/" + context.Request.Files[a].FileName));
clsRequest.Credentials = new NetworkCredential("xxx", "yyy");
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
Stream streamReader = context.Request.Files[a].InputStream;
byte[] bFile = new byte[streamReader.Length];
streamReader.Read(bFile, 0, (int)streamReader.Length);
streamReader.Close();
streamReader.Dispose();
Stream clsStream = clsRequest.GetRequestStream();
//clsStream.Write(bFile, 0, bFile.Length);
// Write the local stream to the FTP stream
// 2 bytes at a time
int offset = 0;
int chunk = (bFile.Length > 2048) ? 2048 : bFile.Length;
while (offset < bFile.Length)
{
clsStream.Write(bFile, offset, chunk);
offset += chunk;
chunk = (bFile.Length - offset < chunk) ? (bFile.Length - offset) : chunk;
}
clsStream.Close();
clsStream.Dispose();
FtpWebResponse response = (FtpWebResponse)clsRequest.GetResponse();
response.Close();
}
}
}
}
UPDATE**
I've changed:
FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(new Uri("ftp://xxxx/yyyy/" + context.Request.Files[a].FileName));
to:
FtpWebRequest clsRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(new Uri("ftp://xxxx/yyyy/" + "staticFileName.zip"));
and
clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
to
clsRequest.Method = System.Net.WebRequestMethods.Ftp.AppendFile;
It appears to upload ok. Need to just test the chunking...
Your link has a section titled "Server-side Handling" which describes that you need reassemble the file yourself. Here is a link to information on appending binary files: How append data to a binary file?
The Plupload documentation mentions that they do the appending on the ChunkUploaded event and piece the file together as it arrives.
Related
Let me start out by saying that I am no pro at web scraping. I can do the basics on most platforms, but that's about it.
I am trying to create the foundation for a web application that can helps users reinforce their language learning by generating additional data, metrics, as well as create new tools for self-testing. The Duolingo website is not offering up any sort of API so my next thought for now is just to scrape https://www.duome.eu/. I wrote a quick little scraper but didn't realize that the site was java. In the following example, it is my wish to collect all of the words from the Words tab that contain anchors:
using System;
using HtmlAgilityPack;
using System.Net.Http;
using System.Text.RegularExpressions;
namespace DuolingoUpdate
{
class Program
{
static void Main(string[] args)
{
string userName = "Podus";
UpdateDuolingoUser(userName);
Console.ReadLine();
}
private static async void UpdateDuolingoUser(string userName)
{
string url = "https://www.duome.eu/" + userName + "/progress/";
// Create the http client connection
HttpClient httpClient = new HttpClient();
var html = await httpClient.GetStringAsync(url);
// Store the html client data in an object
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
//var words = htmlDocument.DocumentNode.Descendants("div")
// .Where(node => node.GetAttributeValue("id", "")
// .Equals("words")).ToList();
//var wordList = words[0].Descendants("a")
// .Where(node => node.GetAttributeValue("class", "")
// .Contains("wA")).ToList();
Console.WriteLine(html);
}
}
}
The html object of the above code contains:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="google" value="notranslate">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Duolingo ยท Podus # duome.eu</title>
<link rel="stylesheet" href="/style.css?1548418871" />
<link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
if("".length==0){
var visitortime = new Date();
var visitortimezone = "GMT " + -visitortime.getTimezoneOffset()/60;
//localStorage.tz = visitortimezone;
//timezone = Date.parse(localStorage.tz);
//timezone = localStorage.tz;
//console.log(timezone);
$.ajax({
type: "GET",
url: "/tz.php",
data: 'time='+ visitortimezone,
success: function(){
location.reload();
}
});
}
});
</script>
</head>
<body>
<noscript>Click here to adjsut XP charts to your local timezone. </noscript>
<!-- Yandex.Metrika counter --> <script type="text/javascript" > (function (d, w, c) { (w[c] = w[c] || []).push(function() { try { w.yaCounter47765476 = new Ya.Metrika({ id:47765476, clickmap:true, trackLinks:true, accurateTrackBounce:true }); } catch(e) { } }); var n = d.getElementsByTagName("script")[0], s = d.createElement("script"), f = function () { n.parentNode.insertBefore(s, n); }; s.type = "text/javascript"; s.async = true; s.src = "https://mc.yandex.ru/metrika/watch.js"; if (w.opera == "[object Opera]") { d.addEventListener("DOMContentLoaded", f, false); } else { f(); } })(document, window, "yandex_metrika_callbacks"); </script> <noscript><div><img src="https://mc.yandex.ru/watch/47765476" style="position:absolute; left:-9999px;" alt="" /></div></noscript> <!-- /Yandex.Metrika counter -->
</body>
</html>
But if you go to the actual url https://www.duome.eu/Podus/progress/, the site contains a ton of script. So upon inspection the first problem is that I am not getting the html that I see in the browser. The second problem is that if you view source, its nothing like what is in inspect and I don't see anything in source that would lead me to isolate the data from div id="words".
Given my lackluster knowledge of java built web pages, how do I do this, or is it even possible?
You can access dualingo profile data in JSON format via https://www.duolingo.com/users/<username>
eg. https://www.duolingo.com/users/Podus
This should be much easier than trying to scrape the duome profile page manually.
I have one html file like Map.Html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<script type="text/javascript" src="http://maps.google.com.mx/maps/api/js?sensor=true"></script>
<script type="text/javascript">
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var address = "Ahmedabad, India" //change the address in order to search the google maps
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var infoWindow = new google.maps.InfoWindow({
content: 'Hello'
});
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.open(map, marker);
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>
then i load this html file in webbrowser control DocumentText using below code:
using (StreamReader reader = new StreamReader(System.Windows.Forms.Application.StartupPath + "\\Map.html"))
{
_mapHTML = reader.ReadToEnd();
}
webBrowser1.DocumentText = _mapHTML;
but map not load properly.
zoom in/out, Map/Satellite option, Marker are showing but one white layer on map display.
SOLVED -- for me at least.
For some reason, the Webbrowser control is defaulting to a bad version (experimental?). I changed my initialization script to specify the current 3.3 version and everything is back to normal.
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.3"></script>
I am generating a string with comma separated variables and trying to export that. The problem is that the whole .html page is getting appended alongwith the exported .csv file.
I have two instances of code, one in local server and other in testing server. I am using the same database in both, the codebase is same in both still, there is an issue while exporting the .csv file.
Following is the code I am using for exporting...
//name - Name of File to save
//text - Comma separated string
private void ExportToCSV(string fileName, string text)
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
if (text != null && text.Length > 0)
{
Response.AddHeader("Content-Length", text.Length.ToString());
Response.Output.Write(text);
}
}
I have tried using Response.Write(text), but still having the same issue.
This is the output I am getting when I get the exported .csv file...
SEARCH DATE,USER TYPE,CITY
20-08-2015,TM,Chicago
20-08-2015,TM,New York
20-08-2015,TM,London
20-08-2015,TM,London
20-08-2015,TM,London
20-08-2015,TM,Cape Town
20-08-2015,TM,Mumbai
20-08-2015,TM,Ottawa
20-08-2015,TM,Mumbai
20-08-2015,TM,Istanbul
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
Search Report
</title>
<script src="Jquery/jquery-1.9.1.js" type="text/javascript"></script>
<script src="Jquery/jquery-ui-1.9.2.custom/js/jquery-ui-1.9.2.custom.js"
type="text/javascript"></script>
<script type="text/javascript">
var t = false;
window.alert = function(message) {
$(document.createElement('div'))
.attr({ title: 'Alert', 'class': 'alert' })
.html(message)
.dialog({
buttons: { OK: function() { $(this).dialog('close'); } },
close: function() { $(this).remove(); },
draggable: true,
modal: true,
resizable: false,
width: 'auto'
});
};
$(function() {
var useragent = navigator.userAgent;
var bName = (useragent.indexOf('Opera') > -1) ? 'Opera' : navigator.appName;
var pos = useragent.indexOf('MSIE');
var bVer = "";
if (pos > -1) {
$('li').has('ul').mouseover(function() {
$(this).children('ul').show();
}).mouseout(function() {
$(this).children('ul').hide();
})
}
});
</script>
.
.
.
.
.
.
</div>
</div>
</body>
</html>
I am unable to figure out the root cause of this issue due to which this is happening. If someone can explain the reason along with the solution, it would be of immense help. Thanks.
you just need to add
Response.End();
i would also add Response.ContentType = "text/csv"; for the correct recognition by the browser
I have an asp.net mvc4 application, in which i'd like to add a treeview . So i used this Jquery library : graphdracula
<html>
<head>
<!-- jQuery -->
<script type="text/javascript" src="~/Content/Scripts/jquery-1.4.2.min.js"></script>
<!-- The Raphael JavaScript library for vector graphics display -->
<script type="text/javascript" src="~/Content/Scripts/raphael-min.js"></script>
<!-- Dracula -->
<!-- An extension of Raphael for connecting shapes -->
<script type="text/javascript" src="~/Content/Scripts/dracula_graffle.js"></script>
<!-- Graphs -->
<script type="text/javascript" src="~/Content/Scripts/dracula_graph.js"></script>
<script type="text/javascript" src="~/Content/Scripts/dracula_algorithms.js"></script>
<script type="text/javascript">
//Show UCLA CS class dependencies (not complete)
$(document).ready(function () {
var width = $(document).width() -500;
var height = $(document).height();
var g = new Graph();
g.edgeFactory.template.style.directed = true;
g.addEdge("Projet", "Fonction1");
g.addEdge("Fonction1", "Sous Fonction 1.1");
g.addEdge("Fonction1", "Sous Fonction 1.2");
g.addEdge("Projet", "Fonction2");
g.addEdge("Fonction2", "Sous Fonction 2.1");
g.addEdge("Fonction2", "Sous Fonction 2.2");
g.addEdge("Fonction2", "Sous Fonction 2.3");
g.addEdge("Fonction2", "Sous Fonction 2.4");
var layouter = new Graph.Layout.Ordered(g, topological_sort(g));
var renderer = new Graph.Renderer.Raphael('canvas', g, width, height);
});
</script>
</head>
<body>
<div id="canvas"></div>
</body>
</html>
the result is a view like this :
it is good but i need to change each title to a link like this : #Html.ActionLink("Projet", "Modify_Project")
How can i modify my snippet to do this task?
If you are using jQuery.
// on page load
$(function(){
// You need to identify a selector that selects all the titles you want to change..
// Likely they all have a certain class
$('.titleSelector').changeElementType("a");
// Now you probably want to add an href
$('a.titleSelector').attr('href','http://youlinkhere');
});
Here is the plugin.. include it after jquery loads and before your script runs https://stackoverflow.com/a/8584217/602379
(function($) {
$.fn.changeElementType = function(newType) {
var attrs = {};
$.each(this[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
this.replaceWith(function() {
return $("<" + newType + "/>", attrs).append($(this).contents());
});
};
})(jQuery);
I am newish to events, and new to flash. I have managed to get files uploaded to my folder using Plupload. I am using a generic handler (New to that too!)
Here's my code:
<style type="text/css">
#import url(/plupload/js/jquery.plupload.queue/css/jquery.plupload.queue.css);
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="http://bp.yahooapis.com/2.4.21/browserplus-min.js"></script>
<script type="text/javascript" src="/plupload/js/plupload.full.js"></script>
<script type="text/javascript" src="/plupload/js/jquery.plupload.queue/jquery.plupload.queue.js"></script>
<script type="text/javascript">
$(function () {
$("#uploader").pluploadQueue({
// General settings
runtimes: 'gears,flash,silverlight,browserplus,html5',
url: '/uploader.ashx',
max_file_size: '10mb',
chunk_size: '1mb',
unique_names: true,
preinit: attachCallbacks,
// Resize images on clientside if we can
//resize: { width: 320, height: 240, quality: 90 },
// Specify what files to browse for
filters: [
{ title: "Image files", extensions: "jpg,gif,png" },
{ title: "Zip files", extensions: "zip" }
],
// Flash settings
flash_swf_url: '/plupload/js/plupload.flash.swf',
// Silverlight settings
silverlight_xap_url: '/plupload/js/plupload.silverlight.xap'
});
// Client side form validation
$('form').submit(function (e) {
var uploader = $('#uploader').pluploadQueue();
// Validate number of uploaded files
if (uploader.total.uploaded == 0) {
// Files in queue upload them first
if (uploader.files.length > 0) {
// When all files are uploaded submit form
uploader.bind('UploadProgress', function () {
if (uploader.total.uploaded == uploader.files.length)
$('form').submit();
});
uploader.start();
} else
alert('You must at least upload one file.');
e.preventDefault();
}
function attachCallbacks(Uploader) {
Uploader.bind('FileUploaded', function(Up, File, Response) {
if ((Uploader.total.uploaded + 1) == Uploader.files.length) {
window.location = '/display.aspx';
}
});
}
});
And then in my code, my handler:
public class uploader : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int chunk = context.Request["chunk"] != null ? int.Parse(context.Request["chunk"]) : 0;
string fileName = context.Request["name"] ?? string.Empty;
HttpPostedFile fileUpload = context.Request.Files[0];
var uploadPath = context.Server.MapPath(GlobalVariables.UploadPath);
using (
var fs = new FileStream(Path.Combine(uploadPath, fileName),
chunk == 0 ? FileMode.Create : FileMode.Append))
{
var buffer = new byte[fileUpload.InputStream.Length];
fileUpload.InputStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, buffer.Length);
}
context.Response.ContentType = "text/plain";
context.Response.Write("Success");
}
public bool IsReusable
{
get { return false; }
}
}
This is working 100%
Problem is, when it complete the upload, nothing happens. It displayed the fact that the files were uploaded.
I need to somehow tell a new method to fire. That is, I need to process the uploaded files. Move them to the right folder ... name them better ... add them to a database, and maybe redirect to the display screen.
So, I need the flash control to tell my code to do something (An event) and also, tell my code which files it can now process (The list of files uploaded).
Is this possible?
Edit: I added the attachCallbacks method, and it's attached. It now redirects to a page, but I need to it somehow send the list of files uploaded to a method (generic handler maybe?) and process the files. How do I redirect to a method?
........
uploader.bind('UploadProgress', function () {
if (uploader.total.uploaded == uploader.files.length)
$('form').submit();
});
uploader.bind('UploadComplete', function (up, files) {
//files are uploaded, call script for each file...etc
});
uploader.start();
..........