| West Wind Internet Protocols 4.68 |
| Uploading a file via HTTP |
oHTTP = CREATEOBJECT("wwHTTP") *** Set mode to multi-part form oHTTP.nHttpPostMode = 2 *** Post a file and a regular form variable oHttp.AddPostKey("File","d:\temp\wws_invoice.pdf",.T.) oHttp.AddPostKey("txtFileNotes","test note") lcHTML = oHTTP.HTTPGet("http://localhost/wconnect/FileUpload.wwd") *** Display result from server ShowHTML(lcHTML)
Note: the file must be picked up by a server application running on the Web Server. In the example above a FileUpload.wwd handler in a Web Connection application is picking up the file which looks something like this:
FUNCTION FileUpload ************************************************************************ * wwDemo :: FileUpload ********************************* *** Function: Demonstrates how to upload files from HTML forms ************************************************************************ lcFileName = "" lcFileBuffer = Request.GetMultiPartFile("File",@lcFileName) lcNotes = Request.GetMultipartFormVar("txtFileNotes") lcFileName = SYS(2023)+"\"+lcFileName IF LEN(lcFileBuffer) = 0 THIS.StandardPage("Upload Error","An error occurred during the upload or the file was too big.") RETURN ENDIF IF LEN(lcFileBuffer) > 500000 THIS.StandardPage("File upload refused",; "Files over 500k are not allowed for this sample...<BR>"+; "File: " + lcFilename) RETURN ENDIF *** Now dump the file to disk File2Var(lcFileName,lcFileBuffer) THIS.StandardPage("Thank you for your file upload",; "<b>File Uploaded:</b> " + lcFileName + ; " (" + TRANSFORM(FileSize(lcFileName),"9,999,999") + " bytes)<p>"+ ; "<b>Notes:</b><br>"+ CRLF + lcNotes ) ENDFUNC * wwDemo :: FileUpload
Here's another example of what C# code in an ASP.Net page might look like:
private void Page_Load(object sender, System.EventArgs e) { // *** Assume Posted variable is "File" HttpPostedFile File = Request.Files["File"]; // Put user code to initialize the page here if (File == null) { this.lblErrorMessage.Text = "No File uploaded"; return; } if (File.ContentLength > 200000) { this.lblErrorMessage.Text = "File too large."; return; } string Filename = Request.PhysicalApplicationPath + "itemimages\\" + (new System.IO.FileInfo(File.FileName)).Name; File.SaveAs(Filename); this.lblErrorMessage.Text = "Upload saved"; }
Note that you can also use the HtmlInputFile control in ASP.NET to handle file uploads. In that case you'd use the Control's PostedFile property to get a reference to the HttpPostedFile object. Otherwise the code is nearly identical:
<input type="file" id="file" runat="server">
protected HtmlInputFile File; private void Page_Load(object sender, System.EventArgs e) { if (File.PostedFile == null) { this.lblErrorMessage.Text = "No File uploaded"; return; } if (File.PostedFile.ContentLength > 200000) { this.lblErrorMessage.Text = "File too large."; return; } string Filename = Request.PhysicalApplicationPath + "itemimages\\" + File.PostedFile.Filename; File.PostedFile.SaveAs(Filename); }
Last Updated: 1/19/2005 |
Send topic feedback