Where do we specify the path for the file to be uploaded to.
Thanks
Printable View
Where do we specify the path for the file to be uploaded to.
Thanks
You have 2 options. It'll post back to the same page if you don't specify PostURL (think of it as setting a form's action attribute). If you use the provided HTTPModule (see the web.config), you can specify ProcessorType and provide a class that implements the IFileUploadProcessor interface or extends AbstractFileUploadProcessor. See the Default.aspx.cs file for more details.
Using the HTTPModule is preferred because it allows you to intercept the request and not buffer the file in memory before storing it. Just add the following to the system.web/httpModules/ node:
<add name="FileUploadModule" type="ServerControls.FileUploadModule, ServerControls"/>
Hi davidhoyt,
I saw the code, great work!
The code works fine, but I have in my code that every Session variable is null.
context.Session is always null, I really don't understand why?
While context.Request.QueryString["myVariable"] works fine.
Have you got an idea?
Thanx a lotCode:
private class FileProcessor : AbstractFileUploadProcessor
{
protected override void processUpload(
HttpContext context,
IPost post,
System.IO.Stream stream,
byte[] buffer,
int bufferSize,
System.Text.Encoding encoding)
{
//Saves files to the current project's directory. You should probably do something more intelligent here.
int read = 0;
byte[] _documentbyte = new byte[bufferSize];
using (FileStream fs = new FileStream(context.Server.MapPath("~/" + post.FileName), FileMode.Create))
{
while ((read = stream.Read(buffer, 0, bufferSize)) >= 0)
fs.Write(buffer, 0, read);
fs.Close();
}
// all fine up to here
if (context.Session["mySessionName"] == null) return;
context.Session["mySessionName"] throws a NullReferenceException !
}
}
Matteo
Yeah, I think I know exactly why. That class is instantiated and run in an HttpModule before the session information is loaded on a postback. I didn't think about sessions because our app requires sessionless connections.
I do have a couple of fixes I've been meaning to upload -- but until the session issue has been fixed, you can always use .AdditionalPostParameters() and pass in whatever you might need.
We use it to pass in the user's ID so we know who to associate the file with.
The reason I wrote the HTTP module was to get at the file being uploaded before it was cached in memory. I'll look into it.
I could still really use some help with the design-time portion of it.
Hi davidhoyt,
thanx a lot for reply, ok I see.
Anyway I solved my problem and adopted the control in my web app and works fine.
Thanx
Matteo
Hi David. Great job with this control. I had upload your solution on Godaddy shared hosting plan and had give folder permission (read/write) and setup virtual directory but I cannot do it works. The error tells me Upload Error. Can you have an idea of what is going on here? As I said I had upload the solution the only thing had change is:
Only a folder call Upload. The local version runs great and it's not a permission problem, I had test with the traditional asp.net FileUpload control and works well.Code:using (FileStream fs = new FileStream(context.Server.MapPath("~/Upload/") + post.FileName, FileMode.Create)) {
Any idea?
Thanks a lot !!!
It could be a trust issue - it does use reflection. I don't have much experience in the shared hosting arena and running websites in partial trust environments. I usually have full control of the site and therefore never any permissions problems.
I'm hoping there's someone out there who could lend a hand with this problem?
I suppose debugging on the server would be difficult - but you could add a bunch of debug statements and output to a file to see if your handler is being called. You could ad-hoc your way into finding out where the code is dying. Once you find that, I might be able to be of more assistance.
In summary, insert debug statements throughout the code until you're able to pinpoint where it's dying. Then let me know the class, method name, and line where it's failing along with any pertinent information.
Hi euclidez,
You should be able to determine if the problem is trust related by adding the following <trust> node to your web.config file and testing locally. Your app *should* fail locally under the same conditions.
Example
As a general rule, I almost always develop under "Medium" trust... especially if the app/site will eventually be hosted on a shared server. If for no other reason than to uncover potential permission issues early on in the development.Code:<system.web>
<trust level="Medium"/>
</system.web>
Not saying this upload thing is 100% a permission issue, but that would be my first guess as well.
Hi all,
I have a problem with AdditionalParameters.
After I save my business object in AjaxEvent, how can I pass the new object ID to the FileUploadDialog parameters?
Steps:
- save business object
- retrieve new object id
- open file dialog and upload file + new object ID
Can you provide a simplified way of passing additional params?
Thanx a lot
Matteo
Just a suggestion,
Depending on what you are trying to accomplish, you may want to change your logic order to
- open file dialog and upload file (process will store to path, or database depending on your requirements)
- save business object with uploaded file path or database id (depends on whether you are saving the file to disc or DB)
- retrieve new object id (if needed for some other process)
You probably don't want to create a new business object until you have
the file uploaded server-side. What if someone starts an upload and
cancels or they lose connection? The server side will be left with a business object that
doesn't contain the intended file.
-MindCore