如何在没有文件上传控件的情况下上传文件 [英] how to upload file without a file upload control

查看:85
本文介绍了如何在没有文件上传控件的情况下上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





有没有办法在不使用文件上传控件的情况下将文件上传到服务器?我的文件路径和文件将始终修复,因此用户不希望每次都使用文件上传控件并选择文件。有没有人对此有所了解?

解决方案





你能试试这个吗? />

 <  表格    action   =  upload.aspx   方法  =  post    enctype   =  multipart / form-data >  
< 输入 < span class =code-attribute> type = file name = UploadedFile / >
< / form > < / form >





代码:

< pre lang =c#> if (Request.Files [ UploadedFile]!= null
{
HttpPostedFile MyFile = Request.Files [ UploadedFile];
// 设置上传文件的位置
string TargetLocation = Server.MapPath( 〜/ Files /);
尝试
{
如果(MyFile.ContentLength > 0
{
// 确定文件名。
string FileName = MyFile.FileName;
// 确定文件大小。
int FileSize = MyFile.ContentLength;
// 创建与文件大小相对应的字节数组。
byte [] FileByteArray = new byte [FileSize];
// 已发布文件被推送到字节数组。
MyFile.InputStream .Read(FileByteArray, 0 ,FileSize);
// 将格式正确的文件上传到服务器。
MyFile.SaveAs(TargetLocation + FileName);
}
}
catch (例外情况)
{
// 处理错误
}
}





另外,您可以看到:使用ASP.NET上传文件 [ ^ ]



或者: http:/ /msdn.microsoft.com/en-us/library/aa478971.aspx [ ^ ]



希望这会有所帮助! :)



问候,

Praneet


Hi,

is there any way to upload a file to server without using a file upload control? my file path and file will always be fix so user don't want to use file upload control and select file every time. does anyone have some idea about this ?

解决方案

Hi,

Can you try this:

<form action="upload.aspx" method="post" enctype="multipart/form-data">
    <input type="file" name="UploadedFile" />
</form></form>



And the code:

if(Request.Files["UploadedFile"] != null)
{
    HttpPostedFile MyFile = Request.Files["UploadedFile"];
    //Setting location to upload files
    string TargetLocation = Server.MapPath("~/Files/");
    try
    {
        if (MyFile.ContentLength > 0)
            {
                //Determining file name. 
                string FileName = MyFile.FileName;
                //Determining file size.
                int FileSize = MyFile.ContentLength;
                //Creating a byte array corresponding to file size.
                byte[] FileByteArray = new byte[FileSize];
                //Posted file is being pushed into byte array.
                MyFile.InputStream.Read(FileByteArray, 0, FileSize);
                //Uploading properly formatted file to server.
                MyFile.SaveAs(TargetLocation + FileName);
            }
        }
     catch(Exception ex)
     {
         //Handle errors
     }
}



Also, you can see this : File Upload with ASP.NET[^]

Or this : http://msdn.microsoft.com/en-us/library/aa478971.aspx[^]

Hope this helps ! :)

Regards,
Praneet


这篇关于如何在没有文件上传控件的情况下上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆