Click here to Skip to main content
15,906,463 members
Home / Discussions / C#
   

C#

 
GeneralRe: New #dev....again [edit] Pin
J. Dunlap25-Oct-03 9:51
J. Dunlap25-Oct-03 9:51 
GeneralRe: New #dev....again [edit] Pin
Blake Coverett26-Oct-03 2:07
Blake Coverett26-Oct-03 2:07 
GeneralC# Network... Need Suggestions. Pin
fadee24-Oct-03 22:35
fadee24-Oct-03 22:35 
GeneralRe: C# Network... Need Suggestions. Pin
Blake Coverett25-Oct-03 20:15
Blake Coverett25-Oct-03 20:15 
GeneralRe: C# Network... Need Suggestions. Pin
Blake Coverett25-Oct-03 23:02
Blake Coverett25-Oct-03 23:02 
GeneralRe: C# Network... Need Suggestions. Pin
fadee25-Oct-03 23:30
fadee25-Oct-03 23:30 
GeneralRe: C# Network... Need Suggestions. Pin
Blake Coverett26-Oct-03 0:19
Blake Coverett26-Oct-03 0:19 
GeneralRe: C# Network... Need Suggestions. Pin
Blake Coverett26-Oct-03 2:05
Blake Coverett26-Oct-03 2:05 
Since I'm in a sample writing mood, here's an example of what I'm describing in the post above.

Usage:

~ Edit upload.aspx and change the savePath to the location you want the files saved in. (Better yet, move the path to your web.config file.)

~ Check the ACLs on that path and make sure the ASPNET user (or whatever user you are running ASP.NET under) has permissions to write files there.

~ Save upload.aspx in your webroot somewhere.

~ Compile client.cs and test with "client http://yourserver/yourpath/upload.aspx testfile.dat"

Notes:

~ Since this uses standard HTTP file uploading, you can test it with a web page as well using an <input type='file'> tag in a form.

~ Also because this is standard HTTP you can easily add security as well, if this is an intranet application have a look at WebClient.Credentials and CredentialsCache.DefaultCredentials.

~ As is, the client is in control of the filename used to save the file. As this permits on client to overwrite files from another it is probably not good idea. Perhaps a folder per client, or generate unique names yourself.

~ If you want to save the files in a database instead of the filesystem, use HttpPostedFile.GetInputStream() instead of HttpPostedFile.SaveAs() and you won't need to write anything to a working directory at all.

~ By default ASP.NET will not let you upload more than 4MB per file, if your situation requires larger files you can change that default in your web.config under system.web/httpRuntime/maxRequestLength.

client.cs
using System;
using System.Net;
using System.Text;
class Upload {
	static int Main(string[] args) {
		try {
			Console.WriteLine(Encoding.UTF8.GetString(
				new WebClient().UploadFile(args[0], args[1])));
			return 0;
		} catch (Exception e) {
			Console.WriteLine(e.Message);
			return 1;
		}
	}
}


upload.ashx
<%@WebHandler class='Upload'%>
using System.IO;
using System.Web;
public class Upload : IHttpHandler {
	const string savePath = @"D:\src\FileTransfer\output";
    public void ProcessRequest(HttpContext context) {
		HttpFileCollection files = context.Request.Files;
		if (files.Count == 0) {
			context.AddError(new HttpException(400, "not multipart/form-data"));
			return;
		}
		for(int i = 0; i < files.Count; ++i) {
			string fileName = Path.Combine(savePath,
				Path.GetFileName(files[i].FileName));
			files[i].SaveAs(fileName);
			context.Response.Write(string.Format(
				"File Saved As {0} ({1} bytes)", 
				fileName, files[i].ContentLength));
		}
    }
    public bool IsReusable { get { return false; }  }
}


-Blake
GeneralRe: C# Network... Need Suggestions. Pin
fadee26-Oct-03 6:03
fadee26-Oct-03 6:03 
GeneralRe: C# Network... Need Suggestions. Pin
Blake Coverett26-Oct-03 6:12
Blake Coverett26-Oct-03 6:12 
GeneralRe: C# Network... Need Suggestions. Pin
fadee26-Oct-03 6:22
fadee26-Oct-03 6:22 
Generalstatic variable Pin
hkl24-Oct-03 10:43
hkl24-Oct-03 10:43 
GeneralRe: static variable Pin
Guillermo Rivero24-Oct-03 10:57
Guillermo Rivero24-Oct-03 10:57 
GeneralRe: static variable Pin
hkl24-Oct-03 10:58
hkl24-Oct-03 10:58 
GeneralRe: static variable Pin
Rocky Moore25-Oct-03 2:48
Rocky Moore25-Oct-03 2:48 
GeneralRe: static variable Pin
leppie25-Oct-03 3:15
leppie25-Oct-03 3:15 
GeneralRe: static variable Pin
hkl27-Oct-03 8:33
hkl27-Oct-03 8:33 
GeneralGenerating a unique serial Pin
Sassan K.Z.24-Oct-03 8:30
Sassan K.Z.24-Oct-03 8:30 
GeneralRe: Generating a unique serial Pin
Guillermo Rivero24-Oct-03 8:35
Guillermo Rivero24-Oct-03 8:35 
GeneralRe: Generating a unique serial Pin
Sassan K.Z.24-Oct-03 9:41
Sassan K.Z.24-Oct-03 9:41 
GeneralRe: Generating a unique serial Pin
leppie25-Oct-03 2:00
leppie25-Oct-03 2:00 
GeneralRe: Generating a unique serial Pin
mistery2225-Oct-03 2:26
mistery2225-Oct-03 2:26 
GeneralRe: Generating a unique serial Pin
Anonymous25-Oct-03 23:33
Anonymous25-Oct-03 23:33 
GeneralOpenFileDialog Pin
Gary Kirkham24-Oct-03 7:56
Gary Kirkham24-Oct-03 7:56 
Generalgetting unicode for a char Pin
Den2Fly24-Oct-03 7:42
Den2Fly24-Oct-03 7:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.